I am working with an ldap object where I am retrieving some entries from Activedirectory. The results are in such a way that the realm is returned in uppercase, like CN=bob,DC=example,DC=com
instead of cn=bob,dc=example,dc=com
. Is there a way to selectively convert the CN
and DC
substrings to lowercase? Sofar, I was using strings.split
multiple times (using "," first and then iterating again using "=") to get to the point where I can get CN, DC, etc. into a list, and then using strings.ToLower on them. Is there a better and smarter way to get this done, possibly using regex so that I can possibly avoid two iterations?
Asked
Active
Viewed 2,092 times
3

scott
- 1,557
- 3
- 15
- 31
-
what about this regex: [Regex Demo](https://regex101.com/r/wN1oF4/1) – Lucas Araujo Apr 19 '16 at 11:42
-
Thank you @lbarros, I could get that match with `([A-Z]{2})=.+,([A-Z]{2})=.+,([A-Z]{2})=.+` but the number of fields could be varying, if an `OU` comes in between. It varies based on the returned entry. – scott Apr 19 '16 at 11:48
2 Answers
6
Here is a regex way to make all uppercase chunks of text followed with a =
tp lower case:
package main
import (
"fmt"
"strings"
"regexp"
)
func main() {
input := "CN=bob,DC=example,DC=com"
r := regexp.MustCompile(`[A-Z]+=`)
fmt.Println(r.ReplaceAllStringFunc(input, func(m string) string {
return strings.ToLower(m)
}))
}
See the Playground demo
The regex - [A-Z]+=
- matches 1 or more uppercase ASCII letters and a =
after them. Then, inside the ReplaceAllStringFunc
, we can use an "anonymous function" to return a modified match value.

Wiktor Stribiżew
- 607,720
- 39
- 448
- 563
-
1Nice and clean. The regex part is converted whereas the other parts, whether upper or lower, remains same. Thank you. – scott Apr 19 '16 at 11:56
0

Uvelichitel
- 8,220
- 1
- 19
- 36
-
The realm part could be anything, and any number, based on the return value. – scott Apr 19 '16 at 11:57