search

Home  >  Q&A  >  body text

Simplification problem of regular expressions in java or scala

There is a requirement to detect whether a string is six eight-digit hexadecimal numbers connected by underscores.
For example: "1234567F_1234567F_1234567F_1234567F_1234567F_1234567F"
I write it myself A regular expression is used to match, as follows:

"^[0-9a-fA-F]{8}_[0-9a-fA-F]{8}_[0-9a-fA-F]{8}_[0-9a-fA-F]{8}_[0-9a-fA-F]{8}_[0-9a-fA-F]{8}$"

This regular expression can be matched successfully, but it feels too complicated and has too much repeated content. Can it be simplified?

学习ing学习ing2800 days ago746

reply all(2)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-12 09:27:26

    I might write like this^([0-9a-fA-F]{8}_){5}[0-9a-fA-F]{8}$.

    Now that the questioner can use {8}, you can consider using the first eight hexadecimal digits + '_' as a number to express the number of occurrences.

    reply
    0
  • 怪我咯

    怪我咯2017-06-12 09:27:26

    You can simplify [0-9a-fA-F] again:

    ^([^\W_]{8}_){5}[^\W_]{8}$

    reply
    0
  • Cancelreply