Regular Expression Not matching expression [duplicate] - python

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)

Related

Regex to find all the words except those which are between special characters [duplicate]

This question already has answers here:
Regular expression to extract text between square brackets
(15 answers)
Java regular expression to extract content within square or round brackets
(3 answers)
Closed 2 years ago.
I am currently learning regex in python and got stuck at a problem in which I have to separate the string such that there are words that are not between any ( ) or [ ].
Let me show an example:
String: Ann Arbor (University of Michigan)[1]
Output: Ann Arbor
This approach uses re.sub to remove anything in brackets and the brackets that surround them.
import re
string = 'Ann Arbor (University of Michigan)[1]'
print(re.sub("[\(\[].*?[\)\]]", "", string))

Multiline string not captured by regex [duplicate]

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)

python regex re.compile and re.search [duplicate]

This question already has answers here:
Regex plus vs star difference? [duplicate]
(9 answers)
Python regular expression pattern * is not working as expected
(2 answers)
Closed 4 years ago.
import re
real_comp = re.compile(r'[0-9]*')
real_comp.search('+123i').group()
Out[7]: ''
I am expecting the the result as "123", but it returns empty.
What's wrong?
You'll need another quantifier, namely a +:
import re
real_comp = re.compile(r'([0-9]+)')
print(real_comp.search('+123i').group())
Which yields
123
Otherwise the regex engine reports a match before the very first consumed char ( [0-9]* is always true).

Why does Python 3 re.findall fail to find all matches here? [duplicate]

This question already has answers here:
How to use regex to find all overlapping matches
(5 answers)
How to find overlapping matches with a regexp?
(4 answers)
Closed 5 years ago.
I'm using Python 3's re.findall to find all occurrences of a substring within a string:
import re
full_string = 'ABCDCDC'
sub_pattern = 'CDC'
re.findall(sub_pattern, full_string)
re.findall only finds ['CDC'], however, the pattern CDC occurs 2 times in the full string.
Why isn't re.findall finding all occurrences of CDC here?
What's needed so that re correctly finds all occurrences?

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