Home > database >  RegEx match everything between 2 characters only if at least 3 characters
RegEx match everything between 2 characters only if at least 3 characters

Time:12-22

I am trying to match all characters between "xAA" and "xFF" but only if theres at least 4 characters between those. Is there a simple way to do this with RegEx?

For example, xAA-12345-xFF should be matched but xAA-1-xFF should be ignored.

My RegEx currently looks like this:

"(?<=\xAA).*(?=\xFF)"

this does match everything between those characters, but i can't find a way on how to only match if at least 4 characters between them, can someone help me?

CodePudding user response:

Quick and dirty hack could be:

"xAA....*xFF"

This requires three (...) characters followed by 0 or more (.*).

CodePudding user response:

Do this:

xAA\S{4,}xFF

\S{4,} matches at least 4 non whitespace characters.

Demo

CodePudding user response:

(?<=xAA-)[0-9]{4,}(?=\-xFF) retracts 12345 from xAA-12345-xFF in case there are only digits

(?<=xAA-)[0-9a-zA-Z]{4,}(?=\-xFF) if letters or digits are possible

regex101.com

  • Related