Capturing a repeated group

后端 未结 9 1530
[愿得一人]
[愿得一人] 2021-01-14 09:17

I am attempting to parse a string like the following using a .NET regular expression:

H3Y5NC8E-TGA5B6SB-2NVAQ4E0

and return the following u

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-14 09:26

    BTW, you can replace [ABCDEFGHJKLMNPQRSTVXYZ0123456789] character class with a more readable subtracted character class.

    [[A-Z\d]-[IOUW]]
    

    If you just want to match 3 groups like that, why don't you use this pattern 3 times in your regex and just use captured 1, 2, 3 subgroups to form the new string?

    ([[A-Z\d]-[IOUW]]){8}-([[A-Z\d]-[IOUW]]){8}-([[A-Z\d]-[IOUW]]){8}
    

    In PHP I would return (I don't know .NET)

    return "$1 $2 $3";
    

提交回复
热议问题