Dienstag, 15. September 2020

Non greedy regular expression

Every time I'm using regular expressions in a script I need to look up the special things. Somethings which bothers me every other quarter or so is to write a non-greedy regexp. I once asked a question on Stackoverflow years ago and received a good response. Here it comes for those who cannot find the SO entry. Assuming the following string from a LDAP entry:
    CN=steffenr,OU=Users,OU=Accounts,DC=big-company,DC=com
I need the part between the '=' and the first comma.

The naive approach in Ruby is

But this gives us "CN=steffenr,OU=Users,OU=Accounts,DC=big-company" because the algorithms is greedy. What we want is to stop before the first comma. The solution is to use a negative look-ahead.

rubular.com offers an interactive console to debug a regexp. Voilá.

Keine Kommentare: