RegEx for finding words between dots [duplicate] - python

This question already has answers here:
How to find overlapping matches with a regexp?
(4 answers)
Closed 4 years ago.
I am new to RegEx and I want to use regular expression to find words between dots.
For example, the text is something like:
abc.efg.hij.klm.opq.
I tried with below RegEx:
\.(\w+)\.
It only show me 2 matches:
.efg.
.klm.
Why am I getting this result?
Here is the link to the RegEx: https://regex101.com/r/pqMN8t/1/

It only shows two matches because the regex engine will not match what it has already matched. After matching .efg., it won't match the dot before hij, because that dot has already been matched (the dot after efg).
One way to fix this is to not match the dots and use lookaheads and lookbehinds instead:
(?<=\.)\w+(?=\.)
This way, the dots won't get matched.

Related

How to get longest match of a pattern of overlapping options? [duplicate]

This question already has answers here:
How to extract longest of overlapping groups?
(4 answers)
Closed 8 months ago.
I am dealing with Python regular expressions where I am trying to get the longest match of a pattern that includes overlapping options.
Consider this example:
import re
task = "s290_fpga_simv_test_verilog"
pattern_str = "(s290|s290_fpga|s289|s289_fpga|s274|s274_fpga)"
result = re.match(pattern_str, task)
print(result.group(1))
It gives me the output s290 where I am expecting the longer s290_fpga. What is necessary to get the longest possible match?
Reverse your order of matches so you become less specific as you go to right. Your code is correct but the re.match() finds a match at s290 and then stops. If you want the result s290_fpgaswap your order to:
"(s290_fpga|s290 etc...)"

python regex filter out exact string [duplicate]

This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 3 years ago.
I'm trying to write a regex that filters out matches if they contain "plex" in them.
plex-release -> should not match
my-release -> should match
potato -> should match
Been playing with pythex and came up with this one that works partially:
(?![plex])(\w+)[-_](release|version)$
However this also messes with any other values containing the letter "p".
I'm trying to come up with a regex that leaves out matches that only contain the string "plex" and in this order, not just any letter from the string.
Yes, you can do it using this regex.
^((?!plex).)*$
Source : Regular expression to match a line that doesn't contain a word

Regex but just in substring [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
I cant find the solution for a regex that looks for a pattern but only in a specific range of the string
I want to find $ $ but only if it is in the 5-7 position of the string and it doesnt matter which character is between those two
Example
xxxx$x$xxxxx would match
xx$x$xxxxxxx would not
import re
should = "xxxx$x$xxxxx would match"
shouldnt = "xx$x$xxxxxxx would not"
pattern = r'^.{4}\$.\$.+'
re.match(pattern, should)
re.match(pattern, shouldnt)
gives
match
None
https://regex101.com/r/RLHrZb/1

Search "bcd" with exclusion of "abcd" using regular expression [duplicate]

This question already has an answer here:
Regex Matching - A letter not preceded by another letter
(1 answer)
Closed 4 years ago.
I need to search one key word, for example, "bcd" or "BCD"; but want to exclude the result of "abcd".
How could I use regex to accomplish this?
What I found on this site now is matching everything but excluding one word.
Try this regex,
(?<!a|A)(bcd|BCD)
This will match a bcd, not after 'a'
Regex
You may try this,
(?i)(?:^|[^a])bcd
You may play with it here,
https://regex101.com/r/jp0lqF/4

Date regex in a sentence [duplicate]

This question already has answers here:
How to match a whole word with a regular expression?
(4 answers)
Closed 4 years ago.
I'm trying to use the date regex from this post:
^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)(?:0?2|(?:Feb))\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$
However, I want to find all matches that are also wrapped around white spaces.
For example in this sentence:
I went to Disney World on 11/11/1989 and once more on 12/12/2009
I want to get back:
11/11/1989
12/12/2009
How do I accomplish this? I'm using Python3 regex module if it matters.
If you want to tweak the regex you linked to work in a string like that, change the three ^ and $s to word boundaries (\b) instead:
\b(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)(?:0?2|(?:Feb))\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))\b|\b(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})\b
https://regex101.com/r/WX5Itv/1

Categories