Remove parenthesis and Characters inside it [duplicate] - regex

This question already has answers here:
Remove text between parentheses in dart/flutter
(2 answers)
Regular expresion for RegExp in Dart
(2 answers)
JavaScript/regex: Remove text between parentheses
(5 answers)
Closed 3 months ago.
I want to remove parenthesis along with all the characters inside it...
var str = B.Tech(CSE)2020;
print(str.replaceAll(new RegExp('/([()])/g'), '');
// output => B.Tech(CSE)2020
// output required => B.Tech 2020
I tried with bunch of Regex but nothing is working...
I am using Dart...

Using Dart, you don't have to use the forward slashes / to delimit the pattern. You can use a string and prepend it with r for a raw string and then you don't have to double escape the backslashes.
In your pattern you have to:
escape the parenthesis
negate the character class to match any character except the parenthesis
repeat the character class with a quantifier like * or else it will match a single character
The pattern will look like:
\([^()]*\)
Regex demo | Dart demo
Example
var str = "B.Tech(CSE)2020";
print(str.replaceAll(new RegExp(r'\([^()]*\)'), ' '));
Output
B.Tech 2020

Your Dart syntax is off, and seems to be confounded with JavaScript. Consider this version:
String str = "B.Tech(CSE)2020";
print(str.replaceAll(RegExp(r'\(.*?\)'), " ")); // B.Tech 2020

Related

The regex to replace the characters in a string before the "-" character [duplicate]

This question already has answers here:
How can I match "anything up until this sequence of characters" in a regular expression?
(15 answers)
Get everything in a string before hyphen in Javascript
(3 answers)
Closed 29 days ago.
I have the following script
function updateProductCode (){
var ss = SpreadsheetApp.getActiveSpreadsheet();
//var sh = ss.getSheetByName('JOBS'); // adjust this to the name of your sheet
const zoneRange = SpreadsheetApp.getActive().getRange('JOBS!P2:P');
const zoneRate = zoneRange.getDisplayValues()
.flat()
.map(value => [value.replace(/.*- ?(.*)/, `$1`)]);
zoneRange.offset(0, 0).setValues(zoneRate);
}
I have the example string "A20W - 20MM Gravel"
I require the output "A20W" of the string, everything before the hyphen including the removal of the one white space. Currently when the script runs the output is everything after the hyphen. Thank you on any help for the regex solution

Create RegEx to find such strings? [duplicate]

This question already has answers here:
Regular expression to match a dot
(7 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 3 years ago.
I am new to RegEx in python. I have created a RegEx formula which should find some special string from text but it is not working as exprected;
def find_short_url(str_field):
search_string = r"moourl.com|ow.ly|goo.gl|polr.me|su.pr|bit.ly|is.gd|tinyurl.com|buff.ly|bit.do|adf.ly"
search_string = re.search(search_string, str(str_field))
result = search_string.group(0) if search_string else None
return result
It should find all the URL shortner from a text. But the su.pr is detecting as surpr from the text. Is there any way to fix it?
find_short_url("It is a surprise that it is ...")
output
'surpr'
It can affect other shortner too. Still scratching my head.
Escape the dots:
search_string = r"moourl\.com|ow\.ly|goo\.gl|polr\.me|su\.pr|bit\.ly|is\.gd|tinyurl\.com|buff\.ly|bit\.do|adf\.ly"
In regex, a dot matches any character. Escaping them makes them match a literal dot.

Regex find sting in the middle of two strings [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 5 years ago.
I want to get the time in the following line. I want to get the string
2017-07-07 08:30:00.065156
in
[ID] = 0,[Time] = 2017-07-07 08:30:00.065156,[access]
I tried this
(?<=[Time] = )(.*?)(?=,)
Where i want to get the string in-between the time tag and the first comma but this doesn't work.
[Time] inside a regex means a T, an i, an m, or an e, unless you escape your square brackets.
You can drop the reluctant quantifier if you use [^,]* in place of .*:
(?<=\[Time\] = )([^,]*)(?=,)

How do I add special characters into a text string using regex.Replace method? [duplicate]

This question already has answers here:
Is there "\n" equivalent in VBscript?
(6 answers)
Closed 5 years ago.
The first character on every line of a file I have is a comma. How can I remove just this comma?
I have tried to use the replace method but it doesn't seem to accept special characters. Here is an example:
myRegExp.Pattern = "\n,"
strText5 =myRegExp.Replace(strText4,"\n")
The above snipper replaces the first new line char and comma with \n. How can I replace with a special character instead of a literal string?
The MSDN library doesn't seem to have the answers I need.
TIA.
If you enable MultiLine mode (and Global if you have not done so) then ^ will match the start of a line:
myRegExp.Pattern = "^,"
myRegExp.Multiline = true
myRegExp.Global = true
strText5 = myRegExp.Replace(strText4, "")
(In a vanilla VB string there are no escape sequences, "\n"
is just the two slash+n characters, for a \n you would use vbLf or chr(10))

RegEx escape function in R [duplicate]

This question already has answers here:
Is there an R function to escape a string for regex characters
(5 answers)
Closed last year.
In a R script, I'd need to create a RegEx that contains strings that may have special characters. So, I should first escape those strings and then use them in the RegEx object.
pattern <- regex(paste('\\W', str, '\\W', sep = ''));
In this example, str should be fixed. So, I'd need a function that returns escaped form of its input. For example 'c++' -> 'c\\+\\+'
I think you have to escape only 12 character, so a conditional regular expression including those should do the trick -- for example:
> gsub('(\\\\^|\\$|\\.|\\||\\?|\\*|\\+|\\(|\\)|\\[|\\{)', '\\\\\\1', 'C++')
[1] "C\\+\\+"
Or you could build that regular expression from the list of special chars if you do not like the plethora of manual backslashes above -- such as:
> paste0('(', paste0('\\', strsplit('\\^$.|?*+()[{', '')[[1]], collapse = '|'), ')')
[1] "(\\\\|\\^|\\$|\\.|\\||\\?|\\*|\\+|\\(|\\)|\\[|\\{)"