[英]Extract sub-string from given String value using regex
I have a requirement where a string needs to be matched and then extract further value from a that string我有一个需要匹配字符串然后从该字符串中提取更多值的要求
I will receive a header in request whose value will be a DN name from ssl certificate.我将在请求中收到一个标头,其值将是来自 ssl 证书的 DN 名称。 Here need to match a specific string 1.2.3.47 in the header and extract remaining text.这里需要匹配标题中的特定字符串 1.2.3.47 并提取剩余的文本。
Sample String passed to method: O=ABC Bank Plc/1.2.3.47=ABC12-PQR-121878, CN=7ltM2wQ3bqlDJdBEURGAMq, L=INDIA, C=INDIA, E=xyz@gmail.com传递给方法的示例字符串:O=ABC Bank Plc/1.2.3.47=ABC12-PQR-121878, CN=7ltM2wQ3bqlDJdBEURGAMq, L=INDIA, C=INDIA, E=xyz@gmail.com
Here is my code:这是我的代码:
private String extractDN(String dnHeader) {
if(!ValidatorUtil.isEmpty(dnHeader)){
String tokens[]=dnHeader.split(",");
if(tokens[0].contains("1.2.3.47")){
int index=tokens[0].lastIndexOf("1.2.3.47");
String id=tokens[0].substring(index+9);
System.out.println(id);
}
}
return id;
}
Can a regex pattern be used here to match and extract value?可以在此处使用正则表达式模式来匹配和提取值吗? Is there any better way to achieve this?有没有更好的方法来实现这一目标? Please help.请帮忙。
If you want to use a pattern and if you know that the value always starts with a forward slash and if followed by one or more digits separated by a dot and then an equals sign, you could use a capturing group:如果要使用模式,并且知道该值始终以正斜杠开头,并且后跟一个或多个由点分隔的数字,然后是等号,则可以使用捕获组:
/[0-9](?:\\.[0-9]+)+=([^,]+)
/
Match /
/
匹配/
[0-9]+
Match 1+ digit 0-9 [0-9]+
匹配1+数字0-9(?:
Non capturing group (?:
非捕获组
\\\\.[0-9]+
match .
\\\\.[0-9]+
匹配.
and 1+ digits 0-9和 1+ 位数字 0-9)+
Close non capturing group and repeat 1+ times )+
关闭非捕获组并重复 1+ 次=
Match =
=
匹配=
([^,]+)
Capture group 1, match 1+ times any char except a ,
([^,]+)
捕获组 1,匹配 1+ 次除 a 之外的任何字符,
Regex demo |正则表达式演示| Java demo Java 演示
For example例如
final String regex = "/[0-9]+(?:\\.[0-9]+)+=([^,]+)";
final String string = "O=ABC Bank Plc/1.2.3.47=ABC12-PQR-121878, CN=7ltM2wQ3bqlDJdBEURGAMq, L=INDIA, C=INDIA, E=xyz@gmail.com";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
Output输出
ABC12-PQR-121878
If you want a more precise match, you could also specify the start of the pattern:如果您想要更精确的匹配,您还可以指定模式的开头:
\\bO=\\w+(?:\\h+\\w+)*/[0-9]+(?:\\.[0-9]+)+=([^,]+)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.