Guillaume D
Guillaume D

Reputation: 2336

cannot find this simple regex

i'm trying with #(?:[a-fA-F0-9]{2}[,])*(?:[a-fA-F0-9]{2}(?!,)){0,1}# to match the following lines:

#1C,B4,97,A3,EF,CF,5A,4A#   //should  match
##  //should  match
#1C#  //should  match
#01# //should match
#1C,1C,1C,1C,# //should not match
#1C,# //should not match
#1C # //should not match
# 1C# //should not match
#11C# //should not match
#11C,,1C# //should not match
#1# //should not match
#ZZ# //should not match

but on regex101 it only matches the first line, why? thank you

Upvotes: 1

Views: 30

Answers (1)

anubhava
anubhava

Reputation: 785376

You may use this regex:

#(?:[a-fA-F0-9]{2}(?:,[a-fA-F0-9]{2})*)?#

RegEx Demo

RegEx Details:

  • #: Match #
  • (?:: Start non-capture group
    • [a-fA-F0-9]{2}: Match 2 hex characters
    • (?:: Start 2nd non-capture group
      • ,: Match a comma
      • [a-fA-F0-9]{2}`: Match 2 hex characters
    • )*: End 2nd non-capture group. * makes this group repeat 0 or more times
  • )?: End non-capture group.? makes this match optional
  • #: Match #

Upvotes: 1

Related Questions