Compartilhar via


Regex 101 Answer I10 - Extract repeating hex blocks from a string

Regex 101 Exercise I10 - Extract repeating hex blocks from a string

Given the string:

PCORR:BLOCK=V5CCH,IA=H'22EF&H'2354&H'4BD4&H'4C4B&H'4D52&H'4DC9;

Extract all the hex numbers in the form “H’xxxx”

*****

You can match the hex digits with:

H'(?<Values>[0-9a-fA-F]{4})

Like our last example, you can call Match() multiple time, use Matches(), or do it in a single call with:

(H'(?<Values>[0-9a-fA-F]{4})&)+

Comments

  • Anonymous
    March 14, 2006
    Actually your last pattern misses the final hex number... unless you do something like

    r.Match(str + "&")

    or

    (H'(?<Values>[0-9a-fA-F]{4})&?)+

    or

    (H'(?<Values>[0-9a-fA-F]{4})(&H'(?<Values>[0-9a-fA-F]{4}))*)+

    (...or does that not work?  it seems suspicious to have two capturing clauses with the same name...)
  • Anonymous
    March 14, 2006
    Er, the + is superfluous on my last regex.  Should be:
    H'(?<Values>[0-9a-fA-F]{4})(&H'(?<Values>[0-9a-fA-F]{4}))

    or better

    H'(?<Values>[0-9a-fA-F]{4}) # first hex number
    (
    __ & # second and further hex numbers are separated with &
    __ H'(?<Values>[0-9a-fA-F]{4}) # second and further hex number
    )
    # further hex numbers are optional
  • Anonymous
    March 16, 2006
    (H'(?<Values>[0-9a-fA-F]{4})[&;])+
  • Anonymous
    June 08, 2009
    PingBack from http://quickdietsite.info/story.php?id=4517