Daniel Cheung
Daniel Cheung

Reputation: 4829

Regex - Split String on `:` but not inside if statements

I am unable to figure out a solution myself on this one. Is it even possible in regex?

I have this sample:

...:...:If...Then...:...:Else...:...:...:IfEnd:...

I want to split my string on colons, but not the colons inside my if-statements.

Something like:

  1. ...
  2. ...
  3. If...Then...:...:Else...:...:...:IfEnd
  4. ...

I searched on other questions, but all of them have 1-character delimiters, which you can solve the problem with [^set]. Is this case possible with regex?

I don't even have a half working regex solution because none of what I tried worked. haha.

If you want to know what I'm doing. I'm attempting to make an application to parse some script. The system will separate statements and parse each line individually. But separating the if-statement/while-loop/for-loop is not going to work properly because of obvious reasons. Is my way of thinking the solution not conventional or isn't right in any chance?

Upvotes: 2

Views: 212

Answers (1)

anubhava
anubhava

Reputation: 785611

Assuming your input doesn't have newlines, you can use:

String str = "...:...:If...Then...:...:Else...:...:...:IfEnd:...";
String[] toks = str.replaceAll("(\\bIf\\b.*?\\bIfEnd\\b):?|:", "$1\n").split("\\n+");

for (String tok: toks) {
    System.err.printf("%s%n", tok);
}

Output:

...
...
If...Then...:...:Else...:...:...:IfEnd
...

This regex first matches text from If to EfEnd and captures it in group #1 OR it matches a colon. Back-reference of captured group #1 is used in replacement while adding \n in front of it.

RegEx Demo

Upvotes: 3

Related Questions