Regex does not support \g character class and duplicate names

I need one of those for more complex stuff. Regexes like ‘“(?.+)”|(?.+)’ are mutually exclusive but do not work. Neither does this work: ‘(?(DEFINE)(?.+)"\g|\g’
Those are valid regxes, but kotlin does not support them. What should I do?

Kotlin does not support regex at all. There are regex implementations in java that you can use. Which one are you using?
Also I don’t think that your regex is valid or did I do something wrong here regex101: build, test, and debug regex?

Stupid forum did mess up my regex. Try regex101: build, test, and debug regex and regex101: build, test, and debug regex. Kotlin does have regex integrated. I’m using kotlin.text.Regex

Not sure if you got a notification. Sorry if you get spammed xD

kotlin.text.Regex is just an alias for JVM’s or JS’ regexes (no clue about native) Regex - Kotlin Programming Language

So what is supported depends on JS/JVM hence, you should refer to Java’s or JS’ regex documentation.

Woops I didn’t know about that one. I’m not really familiar with \g in regex but based on your link I’m guessing that you are trying to do something with recursion. As far as I know this is a feature that isn’t supported by all regex engines. Theoretically it’s not even a regular expression at that point.

\g<name> does just repeat the “subregex” contained in the group called ‘name’. It is basically just an alias so you don’t have to type it out again.

Why shouldn’t it be a regex anymore?

I have posted the problem I want to solve on stackoverflow. It would be nice if there was an easy way to do this.

I think I have found a way on how I could do this. It is super hacky and it involves to use another regex that finds all named groups and renames duplicate names, and then modifies the result, mapping the renamed groups back.

Short answer is that you can use recursion to match matching braces, which is something a regular expression (as formaly defined can’t do).

If I remember correctly a regular expression is formerly defined to only have concatenation(ab), alternation(a | b) and kleene star(a*). All other operations ?, + etc can be build with those 3 basic operations.
You can’t however build recursion from those operations. For that you need a more powerful gramatic (one allowing for recusion). That all said this is all dry language theory and many regex engines support recursion (even though strictly speaking it’s no longer regular at that point).
This is also the reason why you can’t use regex to parse most programming languages (not sure if there are any exceptions, most liekly not).
If you really want to know more about that I suggest Formal grammar - Wikipedia
Regular expression - Wikipedia
or listening in on a computer science lecture (based on where you study this should probably be first or second semester (first for me at the TU Darmstadt Germany)

Good to hear, although at that point I would consider looking into alternatives for regex :wink:

I don’t understand what you mean. I never needed recursion. I just want to have two mutually exclusive groups with the same name.

Sorry, this weired forum doesn’t show if I have answered you correctly, so here is your notification in case you missed it xD

Woops sry, I somehow got confused about \g because regex101 said “recurses the subpattern”. Forget everything I said. I thought \g allowed for recursion in regex. Somehow I did not make the connection to the group. My bad. That comes from only using regex in theory and not much in practice :man_facepalming:

What I was talking about was the scenario where you match the msg group inside of a msg group, but this is not the case with your regex.

You see the name of the person you are replying to in the top left, above the text box. If you want to reply to a person instead of just the topic you need to press reply on their post instead of the blue reply button on the bottom. But don’t worry too much. I’m here often enough that I see posts ansewering me

Great. I have to use java 8 for kotlin to support named types. My android project is on jdk7. :frowning_face:

See documentaton:
https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html

Comparison to Perl 5
Perl constructs not supported by this class:

  • The backreference constructs, \g{n} for the nth capturing group and \g{name} for named-capturing group.

Kotlin does not implement its own regex engine, it uses underlying JVM implementation. So your problem is not with Kotlin, but with JVM.

I believe there are alternative regex engine implementations, but not sure about their quality or readiness for Android.

Or you can think about reformulating your regex without using \g construct.

You can try to rip java.util.regex.Pattern implementation from latest JDK, change package to something like java8.util.regex for all classes and use it.