Home > Back-end >  Pattern for matching a rule
Pattern for matching a rule

Time:04-26

I have tried to write the htaccess for the set of rules.below is the rule pattern I want to match

0x00|0x01|0x02|0x03|0x04|0x05|0x06|0x07|0x08|0x09|0x0A|0x0B|0x0C|
0x0D|0x0E|0x0F|0x10|0x11|0x12|0x13|0x14|0x15|0x16|0x17|0x18|0x19|
0x1A|0x1B|0x1C|0x1D|0x1E|0x1F|0x7F

I have tried as 0x([01-09]|[0A-0F]|[10-19]|[1A-1F]|7F)

CodePudding user response:

0x00|0x01|0x02|0x03|0x04|0x05|0x06|0x07|0x08|0x09|0x0A|0x0B|0x0C| 0x0D|0x0E|0x0F|0x10|0x11|0x12|0x13|0x14|0x15|0x16|0x17|0x18|0x19| 0x1A|0x1B|0x1C|0x1D|0x1E|0x1F|0x7F

This is already a regular expression. You can use this exactly as written (as part of a regex).

But it can be simplified to:

0x([01][0-9A-F]|7F)

CodePudding user response:

You've misled yourself because as a human, you know that "01", "02", etc are a sequence of numbers. Regular expressions don't care about numbers, they care about characters. [01-09] doesn't mean "01 to 09", it means "0; or 1 to 0; or 9" - which clearly doesn't make sense.

If you break it down character by character, you have:

  • "0"
  • "x"
  • "0" or "1" (in regex terms, the set [01])
  • "0" to "9" (the character range [0-9]), or "A" to "F" ([A-F])
  • plus the special case of "0x7F"

So you could for instance write:

0x[01]([0-9]|[A-F])|0x7F

You can combine the two ranges into one, giving:

0x[01][0-9A-F]|0x7F

Sharing the 0x doesn't actually make it any shorter:

0x([01][0-9A-F]|7F)
  • Related