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
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,...
Answer
#2: Post edited
- 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
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* 😉