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

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))

Related

Remove parenthesis and Characters inside it [duplicate]

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

Regex express begin & end specific words[No duplicate] [duplicate]

This question already has answers here:
Regex matching beginning AND end strings
(6 answers)
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I'm trying to write a regex represent 'recognizes words that begin and end in "t".'
I think that the below code is true.
var re = /^t+t*t$/
But it shows 'false'
e.g.
re.test('triplet')
re.test('thought')
re.test('that')
why doesn't my answer solve the above string?
and what is the proper regex?
Your regex is wrong, as pointed out in the comments.
A naive approach could be to check if the entire word starts with t, has any number of any character and then ends with t:
var re = /^t.*t$/
of course, you could also limit the "middle" character to letters:
var re = /^t[a-z]*t$/
However, neither of these approaches check for a word that is a single "t" character. If this is a valid usecase, you'll have to handle it explicitly, e.g.:
var re = /^(t[a-z]*t|t)$/

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.

find a substring after the last backslash with regex [duplicate]

This question already has answers here:
Getting the last backslash in a filepath via regex
(3 answers)
Find the last substring after a character
(4 answers)
Closed 4 years ago.
I need to find the substring after the last \ in the sting (path to file, windows).
I suppose that an elegant pythonic way should be possible (without counting "\" or writing a regression method).
str1 = 'qwerty\\asd\\zxc\x\\c'
str2 = 'zz\\z\\x\\c\\v\\b\\n\\m\\m2m\\m3m'
How to edit this line of code?
found_name = re.findall(r'\\(.*?)', mystr)
Currently it returns all after the fist back slash.
This doesn't require a regex, just use split:
>> str1 = 'qwerty\\asd\\zxc\x\\c'
>> str1.split(r'\\')[-1]
'c'
If you have to use regex for some reason then use:
>>> re.findall(r'.*\\(.*)$', str1)
['c']

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] "(\\\\|\\^|\\$|\\.|\\||\\?|\\*|\\+|\\(|\\)|\\[|\\{)"