正则表达式:Return 单个捕获组中的不同匹配顺序
Regex : Return a different ordering of matches in a single capturing group
我正在尝试从智能卡中提取用户身份,我需要匹配此模式:CN=LAST.FIRST.MIDDLE.0000000000
得到这个结果 returned: FIRST.LAST
如果我在自己的代码中这样做,这通常会很容易:
# python example
string = 'CN=LAST.FIRST.MIDDLE.000000000'
pattern = 'CN=(\w+)\.(\w+)\.'
match = regex.search(pattern, string)
parsedResult = match.groups()[1] + '.' + match.groups()[0]
不幸的是,我正在使用 Keycloaks X.509 certmap web form 匹配模式。
我仅限于使用一个正则表达式,正则表达式只能包含一个捕获组。这是一个 HTML 形式,因此这里没有使用实际代码,只有一个正则表达式。
好像我需要有子捕获组,return 首先是第二个匹配组,然后是第一个匹配组,都在主捕获组中。有没有可能做这样的事情?
此外,我假设我们仅限于 Java 支持的任何功能,因为这是应用程序运行的基础。
是的,这是可能的。此表达式可能会帮助您这样做:
CN=([A-Z]+)\.(([A-Z]+)+)\.([A-Z]+)\.([0-9]+)
Demo
正则表达式
如果这不是您想要的表达方式,您可以 modify/change regex101.com 中的表达方式。例如,如果需要,您可以添加减少表达式的边界并大大简化它。例如,这也可以工作:
CN=(\w+)\.(\w+)(.*)
正则表达式电路
您还可以在 jex.im:
中可视化您的表情
Python 测试
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"CN=([A-Z]+)\.(([A-Z]+)+)\.([A-Z]+)\.([0-9]+)"
test_str = "CN=LAST.FIRST.MIDDLE.000000000"
subst = "\2\.\1"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
JavaScript 演示
const regex = /CN=([A-Z]+)\.(([A-Z]+)+)\.([A-Z]+)\.([0-9]+)/gm;
const str = `CN=LAST.FIRST.MIDDLE.000000000`;
const subst = `\.`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
我认为仅靠一个捕获组是不可能的。如果我没看错keycloak的文档,捕获组其实就是正则表达式的结果。因此您可以按原始顺序匹配 FIRST 或 LAST 或两者,但不能重新排序。
我正在尝试从智能卡中提取用户身份,我需要匹配此模式:CN=LAST.FIRST.MIDDLE.0000000000
得到这个结果 returned: FIRST.LAST
如果我在自己的代码中这样做,这通常会很容易:
# python example
string = 'CN=LAST.FIRST.MIDDLE.000000000'
pattern = 'CN=(\w+)\.(\w+)\.'
match = regex.search(pattern, string)
parsedResult = match.groups()[1] + '.' + match.groups()[0]
不幸的是,我正在使用 Keycloaks X.509 certmap web form 匹配模式。 我仅限于使用一个正则表达式,正则表达式只能包含一个捕获组。这是一个 HTML 形式,因此这里没有使用实际代码,只有一个正则表达式。
好像我需要有子捕获组,return 首先是第二个匹配组,然后是第一个匹配组,都在主捕获组中。有没有可能做这样的事情?
此外,我假设我们仅限于 Java 支持的任何功能,因为这是应用程序运行的基础。
是的,这是可能的。此表达式可能会帮助您这样做:
CN=([A-Z]+)\.(([A-Z]+)+)\.([A-Z]+)\.([0-9]+)
Demo
正则表达式
如果这不是您想要的表达方式,您可以 modify/change regex101.com 中的表达方式。例如,如果需要,您可以添加减少表达式的边界并大大简化它。例如,这也可以工作:
CN=(\w+)\.(\w+)(.*)
正则表达式电路
您还可以在 jex.im:
中可视化您的表情Python 测试
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"CN=([A-Z]+)\.(([A-Z]+)+)\.([A-Z]+)\.([0-9]+)"
test_str = "CN=LAST.FIRST.MIDDLE.000000000"
subst = "\2\.\1"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
JavaScript 演示
const regex = /CN=([A-Z]+)\.(([A-Z]+)+)\.([A-Z]+)\.([0-9]+)/gm;
const str = `CN=LAST.FIRST.MIDDLE.000000000`;
const subst = `\.`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
我认为仅靠一个捕获组是不可能的。如果我没看错keycloak的文档,捕获组其实就是正则表达式的结果。因此您可以按原始顺序匹配 FIRST 或 LAST 或两者,但不能重新排序。