简体   繁体   中英

How can I use regex to match a character (') when not following a specific character (?)?

How can I write a regex pattern to split a string by a specific delimiter as long as it's not preceded by a question mark?

I've written a parser that splits an EDIFACT message into segments, composites and elements. But in the EDI standard the question mark is an escape character. So to split this string:

'PRI+2.005:1+9022.5'RAD+RRHANB97+120814'

I can use string.Split('\\''), and then string.split('+') and then string.split(':') to get PRI, 2.005, 1, 9022.5 and so on However, these characters can be escaped by a question mark:

'PRI+2.005?+3.2:1+9022.5'RAD?'R+RRHANB97+120814'

which should now be PRI, 2.005+3.2, 1, 9022.5, RAD'R, RRHANB97.

Can someone help with a regular expression that would match the ' and not the ?'?

Thanks

负面的背后

(?<!\?)'

只需使用\\'而不是尝试做一个regex.Matches ,而是做一个regex.Split

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