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)"
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:
regex for zip-code
(3 answers)
Closed 2 years ago.
I'm trying to write a regex that follows these constraints:
Exactly 5 digits
Sometimes, but not always, followed by a dash with 4 more digits
Zip code needs to be preceded by at least one whitespace
Cannot be at the start of a text
I've arrived at this but it's not giving me the output I want:
r"^[A-Za-z].*\s.*\d{5}(?:[-\s]\d{4})?$"
I would use:
(?<=[ \t])((?:\d{5}(?=[^\d-]|$))|(?:\d{5}-\d{4}(?=[^\d-]|$)))
Demo and explanation
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.
This question already has answers here:
How to replace multiple substrings of a string?
(28 answers)
Closed 4 years ago.
I currently have the below Python code which works well:-
caseURL = r"\\mydomain\abc\lp_t\GB\123456\Original Format"
caseURL = caseURL.replace("lp_t", "lp_i")
caseURL = caseURL.replace("Original Format", "1")
This works fine as said and carries out the below conversion:-
\\mydomain\abc\lp_t\GB\123456\Original Format
\\mydomain\abc\lp_i\GB\123456\1\
This however just seems a bit clumsy. Is there a more pythonesque way to perform these two segment replacements?
Thanks
A similar post already exists:
How to replace multiple substrings of a string?
You can pick one answer from multiple options in the above post.
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