user2739591
user2739591

Reputation: 45

regex find match within the first n items

I have a string of 8 separated hexadecimal numbers, such as:

3E%12%3%1F%3E%6%1%19

And I need to check if the number 12 is located within the first 4 set of numbers.

I'm guessing this shouldn't be all that complex, but my searches turned up empty. Regular expressions are always a trouble for me, but I don't have access to anything else in this scenario. Any help would be appreciated.

Upvotes: 0

Views: 242

Answers (2)

ndnenkov
ndnenkov

Reputation: 36101

^([^%]+%){0,3}12%

See it in action


The idea is:

  • ^ - from the start
  • [^%]+% - match multiple non % characters, followed by a % character
  • {0,3} - between 0 and 3 of those
  • 12% - 12% after that

Upvotes: 2

buckley
buckley

Reputation: 14089

Here you go

^([^%]*%){4}(?<=.*12.*)

This will match both the following if that is what is intended

1%312%..
1%123%..

Check the solution if %123% is matched or not

If the number 12 should stand on its own then use

^([^%]*%){4}(?<=.*\b12\b.*)

Upvotes: 1

Related Questions