This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Python non-greedy regexes
(7 answers)
Closed 3 years ago.
test = '<tag>part1</tag><tag can have random stuff here>part2</tag>'
print(re.findall("<tag.*>(.*)</tag>", test))
It outputs:
['part2']
The text can have any amount of "parts". I want to return all of them, not only the last one. What's the best way to do it?
You could change your .* to be .*? so that they are non-greedy. That will make your original example work:
import re
test = '<tag>part1</tag><tag can have random stuff here>part2</tag>'
print(re.findall(r'<tag.*?>(.*?)</tag>', test))
Output:
['part1', 'part2']
Though it would probably be best to not try to parse this with just regex, but instead use a proper HTML parser library.
Related
This question already has answers here:
How do I validate a date string format in python?
(5 answers)
Closed 6 months ago.
Im trying to check if a user's input is following the pattern integer/integer/integer(like month/day/year) but i dont know how to use exactly the match function to define that the pattern contains "number",then "/",again "number" and "/"...
Check out https://regex101.com/ for a neat website to check your regex! This is implemented in python using the re library. https://docs.python.org/3/library/re.html
In your case, the pattern would be [0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2,4}
This question already has answers here:
How to split long regular expression rules to multiple lines in Python
(6 answers)
Closed 4 years ago.
Just a simple question. Lets say i have a very long regex.
regex = "(foo|foo|foo|foo|bar|bar|bar)"
Now i want to split this regex into multiple lines. I tried
regex = "(foo|foo|foo|foo|\
bar|bar|bar)"
but this doesnt seems to work. I get different outputs. Any ideas?
Just do it like this
regex = "(foo|foo|foo|foo" \
"|bar|bar|bar)"
This question already has answers here:
Find substring in string but only if whole words?
(8 answers)
Closed 4 years ago.
I am trying to exclude the word ‘define’ without excluding other forms of the word like ‘defined’ or ‘defining’ but the below mentioned regex doesn’t work. Help.
Regex :
^((?!define).)*$
Use word boundaries around the word define:
^((?!\bdefine\b).)*$
You could also write this pattern as:
^(?!.*\bdefine\b).*$
Demo
This question already has answers here:
Does Python have a string 'contains' substring method?
(10 answers)
Closed 5 years ago.
For example if a string contains:
odfsdlkfn dskfThe Moonaosjfsl dflkfn
How can I check to see if it contains "The Moon"?
What I have currently been doing is (but does not work):
if string.find("The Moon")!=-1:
doSomething
Is there anyway to do this?
Thanks!
Simple:
string = 'odfsdlkfn dskfThe Moonaosjfsl dflkfn'
if 'The Moon' in string:
dosomething
you could use regular expressions:
import re
text = "odfsdlkfn dskfThe Moonaosjfsl dflkfn"
if re.find("The Moon", text):
...
and in this case, you could ingore casing with re(pattern, text, re.IGNORECASE) if needed.
This question already has answers here:
Regular expression - starting and ending with a character string
(3 answers)
Closed 5 years ago.
I am trying to do something like
term3_pattern = re.compile(r'(Sanskar:214) * <SP>')
and then check like that
term3_pattern.match(i):
Can someone help me with the regex pattern?
hey you can try this
import re
re.findall(r'Sanskar:214.*<SP>', s) // s is your string