3

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

reckface
  • 5,678
  • 4
  • 36
  • 62

2 Answers2

6

With negative lookbehind:

(?<!\?)'
spender
  • 117,338
  • 33
  • 229
  • 351
0

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

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
Tony Danby
  • 1
  • 1
  • 1