Regex : Return a different ordering of matches in a single capturing group

Luke Pafford :

Im trying to extract user identities from a smartcard, and I need to match this pattern: CN=LAST.FIRST.MIDDLE.0000000000

And have this result returned: FIRST.LAST

This would normaly be easy if I were doing this in my own code:

# python example
string = 'CN=LAST.FIRST.MIDDLE.000000000'
pattern = 'CN=(\w+)\.(\w+)\.'
match = regex.search(pattern, string)

parsedResult = match.groups()[1] + '.' + match.groups()[0]

Unfortunately, I am matching a pattern using Keycloaks X.509 certmap web form. I am limited to using only one regular expression, and the regular expression can only contain one capturing group. This is an HTML form so there is no actual code used here, just a single regular expression.

It seems as if i need to have sub capturing groups, and return the second matched group first, and then the first matched group, all within the main capturing group. Is it possible for something like this to be done?

Also, I assume we are limited to whatever features are supported by Java because that is what the app runs on.

Stephan Kulow :

I don't think this is possible with just one capturing group. If I read the documentation of keycloak correctly, the capturing group is actually the result of the regular expression. So you can either match FIRST or LAST or both in the original order, but not reorder.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=329410&siteId=1