0

I need to extract the values Value1, ar online and PR from the following string:

CN=Value1, OU=ar online, L=PR,

I tried to use the expression [=][a-zA-z][^\,]+, but it always return =Value1, =ar online and =PR, and doesn't produce the result I need.

Any tips about how to solve this issue?

Appreciate all the help.

ramon
  • 23
  • 2
  • Use a capturing group - `=([a-zA-Z][^,]+)`, or a lookbehind (`(?<==)[a-zA-Z][^,]+`). – Wiktor Stribiżew Mar 20 '18 at 14:06
  • And if you can use capturing groups and the three values always appear in the same order, it's as easy to capture them all at once : `.*CN=([^,]+).*OU=([^,]+).*L=([^,]+)` then refer to the first capturing group for the CN, the second for the OU and the third for the L – Aaron Mar 20 '18 at 14:09
  • And depending on the regex engine used you can use named capturing groups to improve readability. For instance with python 3 : https://ideone.com/qqsiXI – Aaron Mar 20 '18 at 14:16
  • Which language are you using? I'm assuming .NET, but you haven't added a language tag, so it's not clear whether or not some regex tokens are available to you (such as lookbehinds) – ctwheels Mar 20 '18 at 14:21
  • I'm using .NET. I didn't add a language tag because I thought it would would not be necessary for this kind of question. (I'm pretty new to Stack Overflow). Anyway, all the given answers worked like a charm. Thank you! – ramon Mar 20 '18 at 14:44

1 Answers1

1

Use a positive look-behind.

(?<=[A-Z]=)[^,]+

That will match one or more characters except , (greedily), preceded by a letter and =.

You can try it online here.

O.O.Balance
  • 2,930
  • 5
  • 23
  • 35