简体   繁体   中英

RegEx for a specific string pattern

Using C#, I will be handling character arrays of info, looking for the following pattern:

a pipe (0x7C), 2 to 7 pairs of characters, followed by another pipe (0x7C).

Stated another way:

|1122[33][44][55][66][77]|

The character pairs consist of characters whose range is from 33-124 decimal ( '!' to '|').

Pairs 3 through 7 are optional, but occur in order, if they occur, so you could have

    |1122| <---shortest
    |112233|
    |11223344|
    |1122334455|
    |112233445566|
    |11223344556677| <---longest

I want to 1) find out if this pattern exists in the character array, 2) extract the individual pairs. These tasks can be separate. I think the best approach to this would be a RegEx, but so far I haven't been able to dream-up an expression to get the job done.

Is a RegEx the way to go and what would a solution for the RegEx itself be?

Is there a better way?

Chuck

If I understand your question correctly the correct pattern would be:

\|([!-|]{2}){2,7}\|

Or to capture each set

\|([!-|]{2})([!-|]{2})([!-|]{2})?([!-|]{2})?([!-|]{2})?([!-|]{2})?([!-|]{2})?\|

Not sure if the range will work directly like that or not, so you may need to do [A-Za-Z!@#$......] if the simplified range doesn't work

Also, I think you don't want to include pipe( | ) in the range as it could mess up the rest so [!-{] might be better

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM