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

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']

Related

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.

Extract text between slashes which contain '=' [duplicate]

This question already has answers here:
filter a List according to multiple contains
(3 answers)
Closed 3 years ago.
I want a regex to extract the text between slashes which contain an equals '='
data/xx/yy/zz/date=20190506/xxx.json
-> date=20190506
Regex not needed.
val str = "data/x=x/yy/zz/date=20190506/xxx.json" //example string (modified)
str.split("/").filter(_.contains('='))
//res0: Array[String] = Array(x=x, date=20190506)

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 to match all character groups in a string [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 5 years ago.
I need a regex to match the groups of characters in a string.
For example this is-a#beautiful^day.
Should result in the following list: this, is, a, beautiful, day.
As a mention I don't know how long the string is or by what characters the words are separated.
Any ideas? I have no clue how to build a regex for this.
If you want find all groups of letters:
import re
string = "this is-a#beautiful^day"
list = re.findall(r'[A-Za-z]+', string)
print list
['this', 'is', 'a', 'beautiful', 'day']