get strings between 2 delimiter in python [duplicate] - python

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I would like to get, from the following string "/path/to/%directory_1%/%directory_2%.csv"
the following list: [directory_1, directory_2]. I would like to avoid using split by "%" my string. I was hoping to find a regex that could help me. However I cannot find the correct one.
For now, I have the following:
re.findall('%(.*)%', dirty_arg)
which output ["directory_1%/%directory_2"]
Do you have any recommandation about that?
Thank you very much for your help.

Try this:
import re
regex = r"%(.*?)%"
dirty_arg = "/path/to/%directory_1%/%directory_2%.csv"
print(re.findall(regex, dirty_arg))
I've added ? to your regex which makes sure it matches as few times as possible. The output of this code is ['directory_1', 'directory_2']

Related

reversing a section of string in python with [start:stop:step] [duplicate]

This question already has answers here:
Understanding negative steps in list slicing
(2 answers)
Closed 1 year ago.
I am learning python and I can't solve this problem:
I am ok to reverse the whole string but I want to get only the "Hello" part
astring = "Hello world!"
I was expecting print(astring[0:4:-1]) would do the work but it does not.
print(astring[5:0:-1]) is better but the H is still missing. I get "olle"
Is there anyway to solve this?
Thank you,
Try this:
print(astring[4::-1])

Python: Using Regex to remove multiple occurrences of punctuation? [duplicate]

This question already has answers here:
strip punctuation with regex - python
(4 answers)
Closed 2 years ago.
I'm looking to remove reoccurring punctuation in a row.
E.g turn 'Hello...' into 'Hello.'
I've been reading some of the documentation on the matter, but am struggling to find a definitive method. (I personally find the docs on regex to a be a little overwhelming, and unclear at times).
I thought it may be something along the lines of:
re.sub('[!()-{};:,<>./?##$%^&*_~]+', '', input)
But this doesn't work. Any help? Thanks.
You can use this:
import re
input='Hello...'
re.sub(r'(\W)(?=\1)', '', input)
Output:
'Hello.'

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

What are () (parentheses) are for in regex python [duplicate]

This question already has answers here:
Python regex -- extraneous matchings
(5 answers)
Closed 6 years ago.
I searched in all the internet and didnt get a good answer on this thing.
What parentheses in python are stand for? its very wierd..
For example, if i do:
re.split(r'(/s*)', "ho from there")
its will give me a list of separate words with the spaces between that... how does its happening?
This isn't specific to python, but in regex those denote a capture group.
Further information on how these are handled in re.split can be seen here

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