Python: Regex unable to find any matches on string [duplicate] - python

This question already has answers here:
How can I find all matches to a regular expression in Python?
(1 answer)
Python regular expression re.match, why this code does not work? [duplicate]
(1 answer)
Closed 4 years ago.
I'm having trouble to find the matches with the above code:
sample="""[2019-01-02 16:15:17.882][P:1624/T:1420][UIPCall.cpp:743
CUIPCall::HandleUICEvent()][Enter]
[2019-01-02 16:15:17.883][P:1624/T:1420][UIPCallState.cpp:1776
CUIPCallIncomingLine1State::HandleUICEvent()][Enter]"""
pattern=r'\[(.*?)\]\[(.*?)\]\[(.+?)(HandleUICEvent|FastNtfClosed_Line1_Common|Login|Logout)\(\)\]\[(.*?)\]$'
p= re.compile(pattern, re.MULTILINE | re.DOTALL)
p.match(sample)
it is troubling me because it works on https://regex101.com/r/hw7pyY/1 but does not match anything on python.
It has to be re.match() as I need the .end() and .start() functions.

Related

Python's regex '|' (or) operator [duplicate]

This question already has answers here:
python re.findall() with substring in alternations
(1 answer)
How can I find all matches to a regular expression in Python?
(1 answer)
Order of regular expression operator (..|.. ... ..|..)
(1 answer)
Closed 2 years ago.
According to Python's documentation for regular expression syntax for | (or) operator: "...once A matches, B will not be tested further, even if it would produce a longer overall match. In other words, the '|' operator is never greedy."
I have tried this in my console (running Python 3.7.6):
import re
txt = 'tim is walking and tom is running'
pattern = 'tim|tom'
re.findall(pattern, txt)
and I get:
['tim', 'tom']
Why is the right side of | still evaluated in this case?

Name nested regex expression [duplicate]

This question already has answers here:
Reuse part of a Regex pattern
(6 answers)
Is it possible to define a pattern and reuse it to capture multiple groups?
(1 answer)
Closed 2 years ago.
I'm trying to match strings of the form:
"FLOAT:FLOAT:FLOAT"
where:
FLOAT="(\d*\.\d*)"
I would like to not repeat code. (eg. "(\d*\.\d*):(\d*\.\d*):(\d*\.\d*)")
Is there support for this in regex? Or is this typically done with string manipulation (.format())?
I'm using python, but I'm interested in other flavors of regex as well.

Regex but just in substring [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
I cant find the solution for a regex that looks for a pattern but only in a specific range of the string
I want to find $ $ but only if it is in the 5-7 position of the string and it doesnt matter which character is between those two
Example
xxxx$x$xxxxx would match
xx$x$xxxxxxx would not
import re
should = "xxxx$x$xxxxx would match"
shouldnt = "xx$x$xxxxxxx would not"
pattern = r'^.{4}\$.\$.+'
re.match(pattern, should)
re.match(pattern, shouldnt)
gives
match
None
https://regex101.com/r/RLHrZb/1

Why is the regex "java" not matching "/something.java" using Python's re module? [duplicate]

This question already has answers here:
What is the difference between re.search and re.match?
(9 answers)
Closed 6 years ago.
This is the code:
import re
regex = re.compile('java')
print regex.match('/something.java')
This is the output:
None
Because python match matches from the beginning. see
python -- re.match vs. re.search
you need to use pattern .*java if you want to use match.

re.match in python to match pattern with string [duplicate]

This question already has answers here:
What is the difference between re.search and re.match?
(9 answers)
Closed 7 years ago.
I am trying to match string with mypattern, somehow I do not get correct result. Can you please point where am I wrong?
import re
mypattern = '_U_[R|S]_data.csv'
string = 'X003_U_R_data.csv'
re.match(mypattern, string)
I like to compile the regex statement first. Then I do whatever kind of matching/searching I would like.
mypattern = re.compile(ur'_U_[R|S]_data.csv')
Then
re.search(mypattern, string)
Here's a great website for regex creation- https://regex101.com/#python

Categories