Replace Spaces inside Square Brackets with Regex in Notepad++ [] - sql

I have SQL views such as the below:
SELECT e.employeeid as [EmployeeID], e.employeedescnoid as [Employee Name],
mgr.employeedesc as [Works For], vh.[path] as Hierarchy,
vh.HierarchyLevel1 as [Hierarchy Level 1],
vh.HierarchyLevel2 as [Hierarchy Level 2],
vh.HierarchyLevel3 as [Hierarchy Level 3],
They get much bigger and I am having to replace spaces inside the [] brackets with an underscore. Many examples I find either dont work or also replace spaces outside the brackets - is there a regex code I can use in notepadd++ find and replace to do this.

Here is a way to do the job in multiple steps:
Find what: (?<=\[)(\w+) <-- note the space at the end
Replace with: $1_
Make sure that Regular expression is checked.
Click on Replace All as many time as necessary to replace all the spaces.

Another way to do it in multiple steps. Set the Find what to (\[[^ \[\]]*) ([^\[\]]*\]) and the replace with to be \1_\2. On each step one space between the brackets will be replaced.
The regular expression is decoded as follows:
( Start of first capture group
\[ A real square bracket
[^ \[\]]* Zero or more characters that are not space or square brackets
) End of the first capture
One real space
( Start of second capture group
[^\[\]]* Zero or more characters that are not square brackets
\] A real closing bracket
)
If the text within the square brackets must not contain line breaks you could change the Find what to (\[[^ \[\]\r\n]*) ([^\[\]\r\n]*\])

Related

Square bracket in regexp_replace pattern

I want to use REGEXP_REPLACE with this pattern. but I don't know how to add square bracket in square bracket. I try to put escape character but i did not work. in this screenshot i want to also keep the [XXX] these square bracket. I need to add this square bracket somehow in my pattern. thanks.
Right now the output is this:
MSD_40001_ME_SPE__XXXX__Technical__Specification_REV9_(2021_05_27)_xls
but I want to like that:
MSD_40001_ME_SPE_[XXXX]_Technical__Specification_REV9_(2021_05_27)_xls
I tried the escape character \ but it did not work
You could try this regex pattern: [^][a-z_A-Z0-9()]
SELECT REGEXP_REPLACE('MSD_40001_ME_SPE_[XXXX]_Technical_%Specification_REV#9_(2021_05_27)_xls', '[^][a-z_A-Z0-9()]', '_')
FROM DUAL
To specify a right bracket (]) in the bracket expression, place it first in the list (after the initial circumflex (^), if any).
See demo here
From Regexp.Info,
One key syntactic difference is that the backslash is NOT a metacharacter in a POSIX bracket expression. So in POSIX, the regular expression [\d] matches a \ or a d. To match a ], put it as the first character after the opening [ or the negating ^. To match a -, put it right before the closing ]. To match a ^, put it before the final literal - or the closing ]. Put together, []\d^-] matches ], , d, ^ or -.
How about this different take on the problem? Match one or more of the following: space OR a dash OR a percent sign OR a dollar sign OR an at sign OR a period (escaping the characters that have special regex meaning) and replace with an underscore. Note this takes care of the double underscore after "Technical".
with tbl(data) as (
select 'MSD 40001-ME-SPE-[XXXX] Technical%$Specification#REV9 (2021.05.27).xls' from dual
)
select regexp_replace(data, '( |\-|%|\$|#|\.)+', '_') fixed
from tbl;
FIXED
---------------------------------------------------------------------
MSD_40001_ME_SPE_[XXXX]_Technical_Specification_REV9_(2021_05_27)_xls

SQL - remove all letters (alpha characters) from a string

I'm trying to write a function that removes any occurrence of any of the 26 alphabet letters from a string.
In: 'AA123A' -> Out: '123'
In: 'AB-123-CD% -> Out: '-123-%'
All I can find on Google is how to remove non-numeric characters, which all seem to be formed around defining the numbers you want to keep. But I want to keep any symbols too.
The 'simple' answer is 26 nested REPLACE for each letter, but I can't believe there isn't a better way to do it.
I could define a string of A-Z and loop through each character, calling the REPLACE 26 times - makes the code simpler but is the same functionally.
Does anyone have an elegant solution?
If I understand correctly, you can use TRANSLATE, e.g.:
SELECT REPLACE(TRANSLATE('AB-123- CDdcba%', 'ABCDabcd',' '), ' ', '');
SELECT REPLACE(TRANSLATE('AB-123- CDdcba%', 'ABCDabcd','AAAAAAAA'), 'A', '');
first case trimming also spaces,
second one, preserving existing spaces.
Just add the rest of characters to 'ABCDabcd' argument and keep 'AAAAAAAA' same length as the second argument.

new lines are not getting eliminated

I'm trying to replace newline etc kind of values using regexp_replace. But when I open the result in query result window, I can still see the new lines in the text. Even when I copy the result, I can see new line characters. See output for example, I just copied from the result.
Below is my query
select regexp_replace('abc123
/n
CHAR(10)
头疼,'||CHR(10)||'allo','[^[:alpha:][:digit:][ \t]]','') from dual;
/ I just kept for testing characters.
Output:
abc123
/n
CHAR(10)
头疼,
allo
How to remove the new lines from the text?
Expected output:
abc123 /nCHAR(10)头疼,allo
There are two mistakes in your code. One of them causes the issue you noticed.
First, in a bracket expression, in Oracle regular expressions (which follow the POSIX standard), there are no escape sequences. You probably meant \t as escape sequence for tab - within the bracket expression. (Note also that in Oracle regular expressions, there are no escape sequences like \t and \n anyway. If you must preserve tabs, it can be done, but not like that.)
Second, regardless of this, you include two character classes, [:alpha:] and [:digit:], and also [ \t] in the (negated) bracket expression. The last one is not a character class, so the [ as well as the space, the backslash and the letter t are interpreted as literal characters - they stand in for themselves. The closing bracket, on the other hand, has special meaning. The first of your two closing brackets is interpreted as the end of the bracket expression; and the second closing bracket is interpreted as being an additional, literal character that must be matched! Since there is no such literal closing bracket anywhere in the string, nothing is replaced.
To fix both mistakes, replace [ \t] with the [:blank:] character class, which consists exactly of space and tab. (And, note that [:alpha:][:digit:] can be written more compactly as [:alnum:].)

Regex to find period in square brackets

I am parsing some SQL statements and have found places where the SELECT statement may be:
SELECT [tblCustomer].[FirstName], [tblCustomer.LastName], [tblOrder].[Order_No]
and as you see the second column has a . inside the square brackets. This is acceptable in Access SQL but not SQL Server. I'm trying to build a RegEx to identify when there is a . inside square brackets and replace it with ].[
I've tried: \[.+?\](?![\.]) which will get me a period inside square brackets but it doesn't stop searching when it finds the closing bracket.
I'm using ECMAScript to be compatible with VBA and I don't have concerns about nested brackets.
Example: https://regex101.com/r/Inxhdg/1/
You can use
Search for: (\[\w+)\.(?=\w+])
Replace with: $1].[
See the regex demo. Details:
(\[\w+) - Group 1 ($1): [ and then any one or more letters, digits, or underscores
\. - a dot
(?=\w+]) - a positive lookahead that requires one or more letters, digits or underscores and then a ] char immediately to the right of the current location.

SQL Server LIKE containing bracket characters

I am using SQL Server 2008. I have a table with the following column:
sampleData (nvarchar(max))
The value for this column in some of these rows are lists formatted as follows:
["value1","value2","value3"]
I'm trying to write a simple query that will return all rows with lists formatted like this, by just detecting the opening bracket.
SELECT * from sampleTable where sampleData like '[%'
The above query doesn't work, because '[' is a special character. How can I escape the bracket so my query does what I want?
... like '[[]%'
You use [ ] to surround a special character (or range).
See the section "Using Wildcard Characters As Literals" in SQL Server LIKE
Note: You don't need to escape the closing bracket...
Aside from gbn's answer, the other method is to use the ESCAPE option:
SELECT * from sampleTable where sampleData like '\[%' ESCAPE '\'
See the documentation for details.
Just a further note here...
If you want to include the bracket (or other specials) within a set of characters, you only have the option of using ESCAPE (since you are already using the brackets to indicate the set).
Also you must specify the ESCAPE clause, since there is no default escape character (it isn't backslash by default as I first thought, coming from a C background).
E.g., if I want to pull out rows where a column contains anything outside of a set of 'acceptable' characters, for the sake of argument let's say alphanumerics... we might start with this:
SELECT * FROM MyTest WHERE MyCol LIKE '%[^a-zA-Z0-9]%'
So we are returning anything that has any character not in the list (due to the leading caret ^ character).
If we then want to add special characters in this set of acceptable characters, we cannot nest the brackets, so we must use an escape character, like this...
SELECT * FROM MyTest WHERE MyCol LIKE '%[^a-zA-Z0-9\[\]]%' ESCAPE '\'
Preceding the brackets (individually) with a backslash and indicating that we are using backslash for the escape character allows us to escape them within the functioning brackets indicating the set of characters.