Regular expression for replacing "[" "]" with single quote " ' " character in string - regex

Hi I am new to regular expression.
I have string like this
"( NATIVE_WHERE_CLAUSE = 'UnitOfMeasure.MeasurementType=[Weight]' ) AND ( NATIVE_RELATION_WHERE_CLAUSE = 'Reference_Name=[Nut to coolent oil]' )"
I tried replace the square brackets [] with single quote ' with replaceAll() method. But it did not work.
Can any one help me what will be regular expression for replacing the square brackets [] with single quote in my above string.

\\[\\] replace [] in a string. And use below to replace [] with '
"( NATIVE_WHERE_CLAUSE = 'UnitOfMeasure.MeasurementType=[Weight]' ) AND ( NATIVE_RELATION_WHERE_CLAUSE = 'Reference_Name=[Nut to coolent oil]' )".replace(new RegExp('\\[|\\]', 'g'), "'");
but that creates two single quotes, one as a child of other. That will not work. So, you must replace with \' to avoid the error as shown below.
"( NATIVE_WHERE_CLAUSE = 'UnitOfMeasure.MeasurementType=[Weight]' ) AND ( NATIVE_RELATION_WHERE_CLAUSE = 'Reference_Name=[Nut to coolent oil]' )".replace(new RegExp('\\[|\\]', 'g'), "\\'");

Related

Matching any combination of space AND newline

I'm trying to find a regexp that catches all instances that contain at least one \n and any number of (space), no matter the order. So, for instance (with spaces denoted with _), all of these should be caught by the regexp:
\n
\n\n\n\n
\n\n\n_\n\n
_\n
\n_
_\n_
_\n\n
\n\n_
_\n\n_
_\n\n_\n
\n_\n_
_\n\n_\n_
___\n__\n and so on...
However, it must not catch spaces that do not border a \n.
In other words, I'd like to reduce all of this (if I'm not making any mistake) to one line:
import re
mystring = re.sub(r'(\n)+' , '\n' , mystring)
mystring = re.sub(r'( )+' , ' ' , mystring)
mystring = re.sub(r'\n ' , '\n' , mystring)
mystring = re.sub(r' \n' , '\n' , mystring)
mystring = re.sub(r'(\n)+' , '\n' , mystring)
mystring = re.sub(r'(\n)+' , ' | ' , mystring)
[ ]*(?:\n[ ]*)+
or, if you want to match tabulations:
[ \t]*(?:\n[ \t]*)+
Demo & explanation
You can use the following regular expression:
(( )*\n+( )*)+

Python regular expression to find and replace multiple matches

I have a string as follows which can have any number of spaces after the first [ or before the last ]:
my_string = " [ 0.53119281 1.53762345 ]"
I have a regular expression which matches and replaces each one individually as follows:
my_regex_start = "(\[\s+)" #Find square bracket and any number of white spaces
replaced_1 = re.sub(my_regex_start, '[', my_string) --> "[0.53119281 -0.16633733 ]"
my_regex_end = "(\s+\])" #Find any number of white spaces and a square bracket
replaced_2 = re.sub(my_regex_end, ']', my_string) -->" [ 0.53119281 -0.16633733]"
I have a regular expression which finds one OR the other:
my_regex_both = "(\[\s+)|(\s+\])" ##Find square bracket and any number of white spaces OR ny number of white spaces and a square bracket
How can I use this my_regex_both to replace the first one and OR the second one if any or both are found?
Instead of catching the brackets, you can replace the spaces that are preceded by [ or followed by ] with an empty string:
import re
my_string = "[ 0.53119281 1.53762345 ]"
my_regex_both = r"(?<=\[)\s+|\s+(?=\])"
replaced = re.sub(my_regex_both, '', my_string)
print(replaced)
Output:
[0.53119281 1.53762345]
Another option you can use aside from MrGeek's answer would be to use a capture group to catch everything between your my_regex_start and my_regex_end like so:
import re
string1 = " [ 0.53119281 1.53762345 ]"
result = re.sub(r"(\[\s+)(.*?)(\s+\])", r"[\2]", string1)
print(result)
I have just sandwiched (.*?) between your two expressions. This will lazily catch what is between which can be used with \2
OUTPUT
[0.53119281 1.53762345]

VB.NET - Regex.Replace error with [ character

I want to remove some characters from a textbox. It works, but when i try to replace the "[" character it gives a error. Why?
Return Regex.Replace(html, "[", "").Replace(",", " ").Replace("]", "").Replace(Chr(34), " ")
When i delete the "[", "").Replace( part it works great?
Return Regex.Replace(html, ",", " ").Replace("]", "").Replace(Chr(34), " ")
The problem is that since the [ character has a special meaning in regex, It must be escaped in order to use it as part of a regex sequence, therefore to escape it all you have to do is add a \ before the character.
Therefore this would be your proper regex code Return Regex.Replace(html, "\[", "").Replace(",", " ").Replace("]", "").Replace(Chr(34), " ")
Because [ is a reserved character that regex patterns use. You should always escape your search patterns using Regex.Escape(). This will find all reserved characters and escape them with a backslash.
Dim searchPattern = Regex.Escape("[")
Return Regex.Replace(html, searchPattern, ""). 'etc...
But why do you need to use regex anyway? Here's a better way of doing it, I think, using StringBuilder:
Dim sb = New StringBuilder(html) _
.Replace("[", "") _
.Replace(",", " ") _
.Replace("]", "") _
.Replace(Chr(34), " ")
Return sb.ToString()

How to escape special regex symbols from string

I am using following regex to match a value in csv string.
for example: csvString = "abc-d, xy%z, efgh, ijklm, nopq(1)rst, uvw#xy"
valueString = "xy%z";
var regExp = new RegExp('(^|, )' + valueString + '(,|$)');
csvString.replace(regExp, "")
Above regExp is works well for any value in csvString except for value 'nopq(1)rst'. It fails when the valueString contains '()' brackets, for example valueString = "nopq(1)rst";. I want the regular expression to match whatever the valueString contains.
How to escape special regex symbols like '(' ')' '[' ']' '\' etc from string
Use the escapeRegex method. It escapes any special characters.
You need to quote your input string to escape all the regex special characters:
var regexpSpecialChars = /([\[\]\^\$\|\(\)\\\+\*\?\{\}\=\!])/g;
var regExp = new RegExp('(^|, )' + valueString.replace(regexpSpecialChars,'\\$1') + '(,|$)');
Reason why nopq(1)rst is failing to match because ( and ) are special regex symbols that are used for grouping, effectively making your regex as:
(^|, )nopq1rst(,|$)

Convert punctuation to space

I have a bunch of strings with punctuation in them that I'd like to convert to spaces:
"This is a string. In addition, this is a string (with one more)."
would become:
"This is a string In addition this is a string with one more "
I can go thru and do this manually with the stringr package (str_replace_all()) one punctuation symbol at a time (, / . / ! / ( / ) / etc. ), but I'm curious if there's a faster way I'd assume using regex's.
Any suggestions?
x <- "This is a string. In addition, this is a string (with one more)."
gsub("[[:punct:]]", " ", x)
[1] "This is a string In addition this is a string with one more "
See ?gsub for doing quick substitutions like this, and ?regex for details on the [[:punct:]] class, i.e.
‘[:punct:]’ Punctuation characters:
‘! " # $ % & ' ( ) * + , - . / : ; < = > ? # [ \ ] ^ _ ` { |
} ~’.
have a look at ?regex
library(stringr)
str_replace_all(x, '[[:punct:]]',' ')
"This is a string In addition this is a string with one more "