使用正则表达式删除字符串两部分之间的文本



我需要使用regex匹配字符串的某些部分,但我很难弄清楚。有问题的字符串看起来总是这样:

CN=Last.First.M.1234567890, OU=OrganizationalUnit, O=Organization, C=CountryName

得到的字符串看起来像CN=1234567890,,所以我只需要得到字符串的第一部分,包括,,并去掉Last.First.M.部分。这能做到吗?

注意:我将这个正则表达式传递到一个我无法触摸的函数中,因此我无法使用更简单的方法,例如拆分字符串或只获取数字并将CN=添加到其中。

谢谢。

当我懒得玩regex时,我会做一些事情。

String[] myStrings = "CN=Last.First.M.1234567890, OU=OrganizationalUnit, O=Organization, C=CountryName"
    .split(",");
// myStrings [0] now contains CN=Last.First.M.1234567890
myStrings[0] = myStrings[0].replace("Last.First.M.", "");
// now we replaced the stuff we didnt want with nothing and myStrings[0]
// is looking pretty nice. This is a lot more readable but probably
// performs worse. For even more readable code assign to variables rather then to modify myStrings[0]

我相信在做了更多的挖掘之后,我找到了我在这里寻找的答案。

跳过捕获组中字符的正则表达式

这里的所有答案都很好,只是不适用于我正在研究的内容。现在我将研究一种不同的方法来解决这个问题。谢谢你的帮助。

使用正则表达式([A-Z]{2})=(?:w+.)+(d+),,您可以获得所需的零件。

最新更新