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.
Related
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
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:].)
Hi I tried using Regex_replace and it is still not working.
select CASE WHEN sbbb <> ' ' THEN regexp_replace(sbbb,'[a-zA-Z _-#]','']
ELSE sbbb
AS ABCDF
from Table where sccc=1;
This is the query which I am using to remove alphabets and specials characters from string and have only numbers. but it doesnot work. Query returns me the complete string with numbers,characters and special characters .What is wrong in the above query
I am working on a sql query. There is a column in database which contains characters,special characters and numbers. I want to only keep the numbers and remove all the special characters and alphabets. How can I do it in query of DB2. If a use PATINDEX it is not working. please help here.
The allowed regular expression patterns are listed on this page
Regular expression control characters
Outside of a set, the following must be preceded with a backslash to be treated as a literal
* ? + [ ( ) { } ^ $ | \ . /
Inside a set, the follow must be preceded with a backslash to be treated as a literal
Characters that must be quoted to be treated as literals are [ ] \
Characters that might need to be quoted, depending on the context are - &
So for you, this should work
regexp_replace(sbbb,'[a-zA-Z _\-#]','')
I am currently debugging an old script and trying to understand a regex in an SQL query.
What would be the result of this search?
[...] REGEXP '(^| |"|\\()ORDER(-| )[[:digit:]]{3}';
Here is a part by part explanation:
(^| |"|\\(): either the beginning of the string (^), or a space, or a double quote, or an opening parenthese (this character needs to be escaped because it is meaningful in regexes)
ORDER: the word "ORDER"
(-| ): either a dash or a space
[[:digit:]]{3}: a sequence of 3 consecutive digits (between 0 and 9)
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]*\])