'Regular Expression, check of substring at position

I have these 2 strings:

  • 00000000000008165736
  • RF77706578000000000543278

I need a regular expression that checks at position 15 that the next 3 characters are 657 or at position 6 the next 3 character are 657

This needs to be one regular expression that matches both strings.

I 've been working on something like this ^.{15}(.{3})|^.{6}(.{3}) but I can't straight it out.



Solution 1:[1]

You may use the following regex pattern:

^(?:.{6}657|.{15}657)

Demo

This regex says to match:

^   from the start of the string
(?:
    .{6}657   657 starting at the 7th character
    |         OR
    .{15}657  657 starting at the 16th character
)

Solution 2:[2]

^(.{6}|.{15})657

For a demo you can see here: https://regex101.com/r/Aj0qG8/1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2