Question

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

Was it helpful?

Solution

With negative lookbehind:

(?<!\?)'

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top