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

2019-05-10 20:50发布

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

2条回答
家丑人穷心不美
2楼-- · 2019-05-10 21:11
我命由我不由天
3楼-- · 2019-05-10 21:24

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

查看更多
登录 后发表回答