Multiline string not captured by regex [duplicate] - python

This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
How to extract an IP address from an HTML string?
(6 answers)
Closed 3 years ago.
When applying regex to a multiline string, I realize the regex may be correct according to rubular.com. The problem is that somehow it is not working against multiline text. Question is, I attempted to do "item.strip()" and no change in behavior.
s = """ #if route rate 230.207.200.1', '', '
(50.50.50.11,230.207.200.1)', """
for item in s:
match = re.findall(r'([0-9].[0-9].[0-9].[0-9],.........)', s)
if match:
print('match')

Do you have any line ending matching or dot character in there?
Try setting the flags to either re.DOTALL or re.MULTILINE, for example:
re.findall(r'...', s, flags=re.DOTALL)

Related

Replace multiple characters using re.sub [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Remove specific characters from a string in Python
(26 answers)
Closed 2 years ago.
s = "Bob hit a ball!, the hit BALL flew far after it was hit."
I need to get rid of the following characters from s
!?',;.
How to achieve this with re.sub?
re.sub(r"!|\?|'|,|;|."," ",s) #doesn't work. And replaces all characters with space
Can someone tell me what's wrong with this?
The problem is that . matches all characters, not the literal '.'. You want to escape that also, \..
But a better way would be to not use the OR operator |, but simply use a character group instead:
re.sub(r"[!?',;.]", ' ', s)

(Python 2.7) Regex Replace with similar character in replacement string as in pattern [duplicate]

This question already has answers here:
Why do backslashes appear twice?
(2 answers)
Closed 3 years ago.
import re
pattern = re.compile(r"/")
a = "a/b"
I tried
re.sub(pattern, '\/', a)
#(also, a.replace('/', '\/'))
#output
a\\/b
What I want is
a\/b
a.replace('/', '\\/')
the first \ is an escape character, so you need to type it twice to have the real \.
You can use if it's not compulsory to use regex:
a = "a/b"
a=a.replace("/","\/")
print(a)

Python regular expression to capture last word with missing line feed [duplicate]

This question already has answers here:
Python csv string to array
(10 answers)
In regex, match either the end of the string or a specific character
(2 answers)
Closed 3 years ago.
I need to capture words separated by tabs as illustrated in the image below.
The expression (.*?)[\t|\n] works well, except for the last line where a line feed is missing. Can anyone suggest a modification of the regular expression to also match the last word, i.e. Cheyenne? Link to code example
Replace [\t|\n] with (\t|$).
BTW, [\t|\n] is a character class, so the pipe | is literal here. You probably meant [\t\n].

Regular Expression Not matching expression [duplicate]

This question already has answers here:
Regular expression to return text between parenthesis
(11 answers)
Regular expression to extract text between square brackets
(15 answers)
Closed 4 years ago.
I am trying to find anything between [ and ] Here is what I have so far and isn't working. I am aware that I can use string.find("[") to get the positions but I am trying to get better at using regex.
string = '[12/Aug/2000:06:29:11 -]'
pattern = re.compile(r'[\[][.][\]]')
matches = pattern.finditer(string)
for match in matches:
print(match)

Strange behavior of re.match function in Python 3 [duplicate]

This question already has answers here:
What is the difference between re.search and re.match?
(9 answers)
Python regular expression not matching
(3 answers)
Closed 6 years ago.
In the following code, I expect the program to print "Match", because "\D+\d" matches the "x4" part of the string. But it does not print anything. What is the problem?
import re
pattern = r"\D+\d"
if re.match(pattern, "1x4"):
print("Match");
Thanks
Your assumption that re.match can match anywhere inside a string is wrong.
https://docs.python.org/2/library/re.html#re.RegexObject.match
If zero or more characters at the beginning of string match this regular expression, return a corresponding MatchObject instance. Return None if the string does not match the pattern; note that this is different from a zero-length match.
Use re.search() instead.

Categories

Resources