Regex but just in substring [duplicate] - python

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

Related

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

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.

re.findall return separate non-overlapping results [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
What do 'lazy' and 'greedy' mean in the context of regular expressions?
(13 answers)
Closed 4 years ago.
I am new to Python and I am struggling a bit with regular expressions. If I have an input like this:
text = <tag>xyz</tag>\n<tag>abc</tag>
Is it possible to get an output list with elements like:
matches = ['<tag>xyz</tag>','<tag>abc</tag>]
Right now I am using the following regex
matches = re.findall(r"<tag>[\w\W]*</tag>", text)
But instead of a list with two elements I am getting only one element with the whole input string like:
matches = ['<tag>xyz</tag>\n<tag>abc</tag>']
Could someone please guide me?
Thank you.
You just need to make your capture non-greedy.
Change this regex,
<tag>[\w\W]*</tag>
to
<tag>[\w\W]*?</tag>
import re
text = '<tag>xyz</tag>\n<tag>abc</tag>'
matches = re.findall(r"<tag>[\w\W]*?</tag>", text)
print(matches)
Prints,
['<tag>xyz</tag>', '<tag>abc</tag>']

Extract substring with regex python [duplicate]

This question already has answers here:
What is the difference between re.search and re.match?
(9 answers)
Closed 5 years ago.
I know this is a very frequently asked question, but it's driving me mad.
I want to use regex to match a substring in my string.
line = '##ParameterValue[part I care about]=garbagegarbage'
And I would like to extract the part I care about.
My code looks like this:
import re
line = '##ParameterValue[part I care about]=garbagegarbage'
m = re.match('\[(.*)\]', line)
print m.group(1)
But this gives me an AttributeError: 'NoneType' object has no attribute 'group'
I tested my regex on regex101 and it works. I don't understand why this fails for me.
Change match to search
import re
line = '##ParameterValue[part I care about]=garbagegarbage'
m = re.search('\[(.*)\]', line)
print m.group(1)

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

unable to match this regular expression in python [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
I am trying to match regular expression using python in this code.
CDS_REGEX = re.compile(r'\+CDS:\s*"([^"]+)",\s*(\d+)$')
cdsiMatch = allLinesMatchingPattern(self.CDS_REGEX, notificationLine)
print cdsiMatch
Matching String:
['+CDS: 24', '079119890400202306A00AA17909913764514010106115225140101061452200']
Please help me i am not able to find my mistake,
As #Blckknght said, are you sure you really want to match that string?
What is ([^"]+) supposed to match?
You're looking for " instead of ' (you probably want ['"]).
You're only checking for numbers here: (\d+), but your long string clearly contains A's.

Categories