Regex: match any word (including foobar), but not foo [duplicate] - python

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

Related

How to use regex pattern to match longer string? [duplicate]

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.

Using a regular expression to remove a uppercase letter if it's previous and next letter is lowercase? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I am new in regular expression. I want to remove a uppercase letter if it has lowercase before and after it. If the input is "I wilYl go theXre" then the output should be "I will go there". How can I get it?
You can use a lookaround:
import re
s='I wilYl go theXre'
print(re.sub(r'(?<=[a-z])([A-Z])(?=[a-z])','',s))
# ^ lookbehind ^lookahead
Prints:
I will go there

Python regular expression to capture last word with missing line feed [duplicate]

This question already has answers here:
Python csv string to array
(10 answers)
In regex, match either the end of the string or a specific character
(2 answers)
Closed 3 years ago.
I need to capture words separated by tabs as illustrated in the image below.
The expression (.*?)[\t|\n] works well, except for the last line where a line feed is missing. Can anyone suggest a modification of the regular expression to also match the last word, i.e. Cheyenne? Link to code example
Replace [\t|\n] with (\t|$).
BTW, [\t|\n] is a character class, so the pipe | is literal here. You probably meant [\t\n].

Excluding words using regex without excluding its variants [duplicate]

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

Strange behavior of re.match function in Python 3 [duplicate]

This question already has answers here:
What is the difference between re.search and re.match?
(9 answers)
Python regular expression not matching
(3 answers)
Closed 6 years ago.
In the following code, I expect the program to print "Match", because "\D+\d" matches the "x4" part of the string. But it does not print anything. What is the problem?
import re
pattern = r"\D+\d"
if re.match(pattern, "1x4"):
print("Match");
Thanks
Your assumption that re.match can match anywhere inside a string is wrong.
https://docs.python.org/2/library/re.html#re.RegexObject.match
If zero or more characters at the beginning of string match this regular expression, return a corresponding MatchObject instance. Return None if the string does not match the pattern; note that this is different from a zero-length match.
Use re.search() instead.

Categories

Resources