String myString = "OID.1.2.840.113549.1.9.2=Responsable: Fernando Martínez Coss, L=Cuauhtemoc, ST=Distrito Federal, C=MX, OID.2.5.4.17=06300, STREET="Av. Hidalgo 77, Col. Guerrero", EMAILADDRESS=acods@sat.gob.mx, O=Servicio de Administración Tributaria, CN=A.C. del Servi开发者_运维百科cio de Administración Tributaria";
I have that one, excuse the spanish. How can I get all the characters after the "CN=" The CN propertie might not fall always at the end of the string, and could have any character after it.
It this particular case the CN= falls conveniently at the end of the string, but it is possible that it might end with a comma, or another character that it is not a letter or number.
Like:
CN=A.C. del Servicio de Administración Tributaria/email CN=A.C. del Servicio de Administración Tributaria, Somethings.
I just want specifically the value before the slash and the comma and after the equals.
You could assume that the "CN" element will always be either at the end of the string or followed by another "X=Y" sequence, so perhaps you could try this:
Pattern p = Pattern.compile("CN=(.*?)($|, [A-Z]+)=.*");
Matcher m = p.matcher(myString);
if (m.find()) {
// Now m.group(1) contains the value associated with "CN".
}
String myString = "OID.1.2.840.113549.1.9.2=Responsable: Fernando Martínez Coss, L=Cuauhtemoc, ST=Distrito Federal, C=MX, OID.2.5.4.17=06300, STREET=\"Av. Hidalgo 77, Col. Guerrero\", EMAILADDRESS=acods@sat.gob.mx, O=Servicio de Administración Tributaria, CN=A.C. del Servicio de Administración Tributaria";
Pattern pattern = Pattern.compile("CN=([^,]*)");
Matcher matcher = pattern.matcher(myString);
if(matcher.find())
{
System.out.println(matcher.group(1));
}
You can do this without regexp in this way - especially this can be good if you will want retrieve more parameters
Example 1
String myString = ...;
String[] firstSplit = myString.split(",");
for (int i=0; i<firstSplit.length; i++) {
String[] secondSplit = firstSplit[i].split("=");
if ("CN".equals(secondSplit[0].trim())) {
System.out.println(secondSplit[1]);
}
}
But this will only work if you will use "," only to separate parameters. If you can't treat "," like delimiter then the best way will be regexp or indexof method
String myString = "OID.1.2.840.113549.1.9.2=Responsable: Fernando Martínez Coss, L=Cuauhtemoc, ST=Distrito Federal, C=MX, OID.2.5.4.17=06300, STREET="Av. Hidalgo 77, Col. Guerrero", EMAILADDRESS=acods@sat.gob.mx, O=Servicio de Administración Tributaria, CN=A.C. del Servicio de Administración Tributaria";
int indexCn = myString.indexOf("CN");
int indexComma = myString(",", indexCn );
return indexComma == -1 ? myString.substring( indexCn ) : myString.substring( indexCn, indexComma - indexCn );
You need to handle two cases
- CN is the last element (so no comma afterwards, just end of String)
- CN is in the middle, so we have a comma as a delimiter.
For the updated question:
result = myS.replaceAll (".*CN=", "").replaceAll ("[,/].*", "");
result = result.replaceAll ("[A-Z0-9.]+=", "");
(updated again, to include [,/]
from the updated question).
(updated again, in reaction to newest comment)
精彩评论