I use Java and regular expressions and have to split some data into several objects. In my input, a single quote character (') indicates the end of a UNLESS entity, preceded by an escape character, which is a question mark (?).
My RegEx (?<!\\?)\\' and I use a scanner to split the input into separate objects. Thus, the following cases work correctly:
Hello'There becomes 2 entities: Hello and There Hello?'There remains 1 entity: Hello?'There
However, when I come across a case where I want to avoid a question mark, it does not work. So:
Hello??'There should become 2 entities: Hello?? and There Hello???'There should become 1 entity: Hello???'There Hello????'There should become 2 entities: Hello???? and There Hello?????'There should become 1 entity: Hello????'There Hello?????There should become 1 entity: Hello????There Hello??????There should become 1 entity: Hello?????There
Thus, the rule is that there is an even number of question marks, followed by a quote, it should be divided. If there is an odd number of question marks, then it should not be divided.
Can someone help fix my Regex (hopefully with an explanation!) To handle a few cases?
Thanks,
Phil
source share