Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

80%
+6 −0
Q&A How can I emulate regular expression's branch reset in Java?

I've kinda found a very limited, not so elegant, far from ideal "solution", using replaceAll: String regex = "(?:([aeiou]+)[0-9]+|([123]+)[a-z]+)\\W+"; System.out.println("ae123.".replaceAll(regex,...

posted 4y ago by hkotsubo‭  ·  edited 4y ago by hkotsubo‭

Answer
#2: Post edited by user avatar hkotsubo‭ · 2020-08-11T17:41:25Z (about 4 years ago)
  • I've *kinda* found a **very** limited, not so elegant, far from ideal "solution", using [`replaceAll`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replaceAll-java.lang.String-java.lang.String-):
  • ```java
  • String regex = "(?:([aeiou]+)[0-9]+|([123]+)[a-z]+)\\W+";
  • System.out.println("ae123.".replaceAll(regex, "$1$2"));
  • System.out.println("111abc!!".replaceAll(regex, "$1$2"));
  • ```
  • This prints:
  • > ae<br>
  • > 111
  • The trick is in the second argument: `"$1$2"` means that I'm concatenating groups 1 (`$1`) and 2 (`$2`). Because of the alternation (`|`), only one of the groups is captured and the other will be empty. And when I concatenate them, the result is always the contents of the captured group.
  • ---
  • ### Limitations
  • But as I said, this solution is very limited. Let's suppose the regex is a little bit more complicated with lots of different groups. Something like that:
  • ```none
  • (1) | (2) (3) (4) | (5) (6) | (7) | (8)
  • ```
  • In that case, I can have only group 1, **or** only groups 2, 3 and 4, **or** only groups 5 and 6, **or** only group 7, **or** only group 8.
  • Of course I could still use `replaceAll` with `"$1$2$3$4$5$6$7$8"`, but if the regex matches groups 2, 3 and 4, they will be concatenated and I wouldn't know each group's value individually. Unless I use some separator, such as `"$1,$2,$3,$4,$5,$6,$7,$8"` and then `split` the result, but that would be too "ugly" IMO (not to mention that the separator itself can't be part of the group's value, etc).
  • **If** Java supported *branch reset*, the groups numbering would be like this:
  • ```none
  • (?| (1) | (1) (2) (3) | (1) (2) | (1) | (1) )
  • ```
  • And I'd just need to loop through them, always starting with 1, until `m.groupCount()`.
  • *Which means I'm still waiting for better solutions* 😉
  • I've *kinda* found a **very** limited, not so elegant, far from ideal "solution", using [`replaceAll`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replaceAll-java.lang.String-java.lang.String-):
  • ```java
  • String regex = "(?:([aeiou]+)[0-9]+|([123]+)[a-z]+)\\W+";
  • System.out.println("ae123.".replaceAll(regex, "$1$2"));
  • System.out.println("111abc!!".replaceAll(regex, "$1$2"));
  • ```
  • This prints:
  • > ae<br>
  • > 111
  • The trick is in the second argument: `"$1$2"` means that I'm concatenating groups 1 (`$1`) and 2 (`$2`). Because of the alternation (`|`), only one of the groups is captured and the other will be empty. And when I concatenate them, the result is always the contents of the captured group.
  • ---
  • ### Limitations
  • But as I said, this solution is very limited. Let's suppose the regex is a little bit more complicated with lots of different groups. Something like that:
  • ```none
  • (1) | (2) (3) (4) | (5) (6) | (7) | (8)
  • ```
  • In that case, I can have only group 1, **or** only groups 2, 3 and 4, **or** only groups 5 and 6, **or** only group 7, **or** only group 8.
  • Of course I could still use `replaceAll` with `"$1$2$3$4$5$6$7$8"`, but if the regex matches groups 2, 3 and 4, they will be concatenated and I wouldn't know each group's value individually. Unless I use some separator, such as `"$1,$2,$3,$4,$5,$6,$7,$8"` and then `split` the result, but that would be too "ugly" IMO (not to mention that the separator itself can't be part of the group's value, etc).
  • **If** Java supported *branch reset*, the groups numbering would be like this:
  • ```none
  • (?| (1) | (1) (2) (3) | (1) (2) | (1) | (1) )
  • ```
  • And I'd just need to loop through them, always starting with 1, until [`m.groupCount()`](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/regex/Matcher.html#groupCount()).
  • *Which means I'm still waiting for better solutions* 😉
#1: Initial revision by user avatar hkotsubo‭ · 2020-08-11T16:22:53Z (about 4 years ago)
I've *kinda* found a **very** limited, not so elegant, far from ideal "solution", using [`replaceAll`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replaceAll-java.lang.String-java.lang.String-):

```java
String regex = "(?:([aeiou]+)[0-9]+|([123]+)[a-z]+)\\W+";
System.out.println("ae123.".replaceAll(regex, "$1$2"));
System.out.println("111abc!!".replaceAll(regex, "$1$2"));
```

This prints:

> ae<br>
> 111

The trick is in the second argument: `"$1$2"` means that I'm concatenating groups 1 (`$1`) and 2 (`$2`). Because of the alternation (`|`), only one of the groups is captured and the other will be empty. And when I concatenate them, the result is always the contents of the captured group.

---
### Limitations

But as I said, this solution is very limited. Let's suppose the regex is a little bit more complicated with lots of different groups. Something like that:

```none
(1) | (2) (3) (4) | (5) (6) | (7) | (8)
```

In that case, I can have only group 1, **or** only groups 2, 3 and 4, **or** only groups 5 and 6, **or** only group 7, **or** only group 8.

Of course I could still use `replaceAll` with `"$1$2$3$4$5$6$7$8"`, but if the regex matches groups 2, 3 and 4, they will be concatenated and I wouldn't know each group's value individually. Unless I use some separator, such as `"$1,$2,$3,$4,$5,$6,$7,$8"` and then `split` the result, but that would be too "ugly" IMO (not to mention that the separator itself can't be part of the group's value, etc).

**If** Java supported *branch reset*, the groups numbering would be like this:

```none
(?| (1) | (1) (2) (3) | (1) (2) | (1) | (1) )
```

And I'd just need to loop through them, always starting with 1, until `m.groupCount()`.

*Which means I'm still waiting for better solutions* 😉