This question already has answers here:
How to use regex with optional characters in python?
(5 answers)
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I have a text string, similar to below example,
I have 5-6 year of experience with 2-3 years experience in Java
I have used this below regex syntax to match it,
import re
pattern = '\d{1}-\d{1} year'
[(m.start(0), m.end(0),'Experience') for m in re.finditer(pattern, string)]
# results
5-6 year
2-3 year (In this case it's missing out the 's'.)
How to modify this pattern to also match 'years and year' which every is longest?
Add an optional "s": '\d{1,2}-\d{1,2}\s*years?'. I also changed '\d{1}' to '\d{1,2}' which means "one or two digits" (it's hard to imagine someone has more than 99 years of experience), and replaced one space with '\s*' - any number of spaces, including no spaces.
Related
This question already has answers here:
re.findall behaves weird
(3 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I'm trying to match FD or MD in a string by doing:
matches = re.findall(r"(F|M)D",myString)
Suppose myString = 'MD'. Then, matches becomes
matches = ['M']
Why does it ignore D?
That's because (F|M) is a group, and D is not a part of this group.
Use this instead:
matches = re.findall(r"((?:F|M)D)",myString)
For a visual representation of the differences between these two patterns, I really like to use Regexper.com:
(F|M)D
((?:F|M)D)
The Python documentation on regular expressions has a lot more information available here.
Note that ?: indicates that F|M is a "non-capturing" group. If the pattern were ((F|M)D) instead, then matches would be [('MD', 'M')] (which doesn't sound like what you want).
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:
How to use regex to find all overlapping matches
(5 answers)
Closed 2 years ago.
I have text as below-
my_text = "My telephone number is 408-555-1234"
on which i am searching the pattern
re.findall(r'\d{3}-\d{1,}',my_text)
My intention was to search for three digit numeric value followed by - and then another set of one or more than one digit numeric value. Hence I was expecting the result to be - ['408-555','555-1234'],
However the result i am getting os only ['408-555'] .
Could anyone suggest me what is wrong in my understaning here. And suggest a pattern that would serve my purpose
you can use:
re.findall(r'(?=(\d{3}-\d+))', my_text)
output:
['408-555', '555-1234']
This question already has an answer here:
Restricting character length in a regular expression
(1 answer)
Closed 4 years ago.
So I have a regex that goes like:
regex1= re.compile(r'\S+#\S+')
This works perfectly but I am trying to add a character limit so the total amount of characters have to be less than 20.
I tried re.compile(r'\S+#\S+{5,20}') but it keeps giving me an error. Seems like a simple fix, but cant see what I am doing wrong.
You can't specify a greedy modifier (+) with a specific number of characters (i.e., \S+{5,20) is not a valid pattern). If you're doing this in python, I'd suggest just using the len(...) function on the string in addition to the regex to verify. For example:
if regex1.match(email) and (len(email) < 20):
...
This question already has answers here:
Matching all words except one
(8 answers)
Regex: match everything but a specific pattern
(6 answers)
Closed 4 years ago.
I'm very new to regex and I've looked but haven't found the syntax I'm looking for.
I want to match any word (including foobar), but not foo. However, everythimg I've found catches foobar with foo.
What's the correct way to do this? I'm working in Python, if that matters
(?!^foo$)^\w+$
This is a negative look ahead (?!), saying don't match the word foo, but match any other word.
^ and $ assert the start and end of the string, respectively. \w+ means match one or more of any word character.
And an example:
https://regex101.com/r/nfxyso/2