How can I use regex to match a character (') w

2019-05-10 20:49发布

问题:

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

回答1:

With negative lookbehind:

(?<!\?)'


回答2:

Just use \' and instead of trying to do a regex.Matches, do a regex.Split instead