I currently have the regex "(?<!mp){i}"
which matches a specific number but it overmatches and includes 10
, 11
, 21
, etc
when I want to match 1
. In short, it is matching as long as i
contains the number in the string (if i=2
and the string contains "xxxxxx 24" it would also match, which I don't want)
strings
xx 1.mp4
xx 10.mp4
xx21.mp4
xx1.mp4
xx 1 xx.mp4
xx.1.xxx.mp4
xx.11.xxx.mp4
xxx s1e3 xxx.mp4
xxx season 1 episode 3
xxx season 2 episode 1
expected outcome if trying to match where i=1
xx 1.mp4
xx1.mp4
xx 1 xx.mp4
xx.1.xxx.mp4
xxx season 2 episode 1
note: xxx s1e3 xxx.mp4
and xxx season 1 episode 3
was NOT match even though it contains 1
because it was followed by s
and season
. However, xxx season 2 episode 1
Should be match, since the "episode" contains 1
Another note: Ideally, it shouldn't be limited to just 1, since I wouldn't want to match 24
32
when I am trying to find a match for i=2
, etc
I've tried
if re.search(rf"(?<!mp){i}", string):
do something
it returns strings with 10
,11
, 21
100
, etc when I am just searching for 1
. Likewise with i=2
CodePudding user response:
You can use negative lookaheads and lookbehinds to ensure that the number is not adjacent to any other digits.
if re.search(rf"(?<!\d){i}(?!\d)", string):