1

I need to match certain parts of a string using regex and am having a terrible time trying to figure it out. The string in question will always look like this:

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

The resulting string will look like CN=1234567890, so I need to just get the first part of the string up to and including the , and strip out the Last.First.M. part. Can this be done?

Note: I am passing this regex into a function which I cannot touch, so I cannot use easier methods such as splitting the string or getting just the digits and adding the CN= to it.

Thanks.

9
  • What language or tool are you using? Commented Feb 6, 2013 at 21:15
  • 2
    What parameters does this function take exactly, and what does it return? Commented Feb 6, 2013 at 21:16
  • @squiguy I am using java Commented Feb 6, 2013 at 21:17
  • @TimPietzcker The function takes a String value and returns a Pattern from the java.util.regex.Pattern class. Commented Feb 6, 2013 at 21:19
  • What do you mean by that it will always look like that? I'm guessing that you mean that it will always be of the form 'CN=***, OU=***, O=***, C=***', but that the *** can be any other text? Commented Feb 6, 2013 at 21:21

3 Answers 3

1

Here's something I do when I'm to lazy to play around with 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]
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry missed the part that you couldn't cheat. Other answers are better for you!
1

I believe I found the answer to what I am looking for here after doing more digging.

Regular expression to skip character in capture group

All answers here were great, just didn't apply to what I was working on. Now I will work on a different method to solve this. Thanks for all the assistance.

Comments

0

With the regexp ([A-Z]{2})=(?:\w+\.)+(\d+), you can obtain the parts that you want.

3 Comments

This matches the whole CN=Last.First.M.1234567890, rather than just selecting CN=1234567890, when I tried it using regexr.com?33mcn
it captures two groups: CN and 123456789. Then one should just use a variation of @aleroot solution like m.group(1) + "=" + m.group(2)
The OP stated that I am passing this regex into a function which I cannot touch, so I cannot use easier methods such as splitting the string or getting just the digits and adding the CN= to it. So I believe concatenating the groups won't work for this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.