Reputation: 193
I have the following string ...
CN=KendallLDAPTEST Weihe,OU=CORPUSERS,OU=CORP,DC=int,DC=appriss,DC=com
and I need to extract KendallLDAPTEST Weihe
in other words I need to extract all string data between the first =
and the first ,
Upvotes: 0
Views: 4956
Reputation: 92
If the text you are trying to capture is always appearing on the same index you can use this:
the_text[3...22]
Upvotes: 0
Reputation: 160551
I'd use:
str = "CN=KendallLDAPTEST Weihe,OU=CORPUSERS,OU=CORP,DC=int,DC=appriss,DC=com"
str[/=([^,]+)/, 1] # => "KendallLDAPTEST Weihe"
By default the regular expression will find the first match then stop. This simply finds the first =
, then captures all characters that are not ,
until the first ','
and returns it.
Upvotes: 1
Reputation: 1460
Tested, works
Using regex:
str = "CN=KendallLDAPTESTWeihe,OU=CORPUSERS,OU=CORP,DC=int,DC=appriss,DC=com"
str[/\=(.*?),/,1]
For anyone re-using this, replace both instances of here
with the characters you want to find the text in between:
[/\here(.*?)here/,1]
Upvotes: 9
Reputation: 239311
You can simply break the string apart using split
on ,
to first produce an array of key=value
pairs, and then split those pairs on =
.
str = 'CN=KendallLDAPTEST Weihe,OU=CORPUSERS,OU=CORP,DC=int,DC=appriss,DC=com'
pairs = str.split(',').map { |pair| pair.split('=') }
# => [["CN", "KendallLDAPTEST Weihe"], ["OU", "CORPUSERS"], ["OU", "CORP"], ["DC", "int"], ["DC", "appriss"], ["DC", "com"]]
pairs.first[1]
# => "KendallLDAPTEST Weihe"
This is somewhat naive in that it will not work if any of your data contains escaped =
or ,
characters.
Upvotes: 1