-1

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
mimzee
  • 109
  • 1
  • 7
  • 1
    Then maybe you can just do `my_string[5] == '$' and my_string[7] == '$'`? (check first `len(my_string) > 7` if necessary) – jdehesa Mar 13 '19 at 14:39
  • `/^.{4}\$.\$/` ... – Pranav C Balan Mar 13 '19 at 14:39
  • This question, if not great, is nevertheless accurate and perfectly answerable. I really don't see how the generic [Learning Regular Expressions](https://stackoverflow.com/questions/4736/learning-regular-expressions) can be considered a duplicate of this one - unless we consider that any regex question can be answered by 'learn regex'. – Thierry Lathuille Mar 13 '19 at 14:55

1 Answers1

1
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

RnD
  • 969
  • 4
  • 19
  • 48