ab1-cde23-fg45-h6-ijk-789.lmn.local. 86400 IN A 12.34.5.123
In the follow DNS entry, I'm trying to match the h6
section (position 4). At this point, I know this section of the domain is only composed of 2 letters/digits or one of each, so I can match it (in a clumsy way) with
"-[a-zA-Z0-9]{2}-"
In a case where I could not assume that this is the only section of a domain with 2 letters/digits, how could I match only the content of the 4th position minus the -
? (ab1
being the first position, cde23
the second, and so on, with all the positions separated by -
)
I'm able to match up to the 4th positions with the following regex, but it includes everything from the start.
"([a-zA-Z0-9]*-){3}[a-zA-Z0-9]*-"
I'm using theses regexp in golang.
Do:
^(?:[^-]+-){3}([^-]+)
^(?:[^-]+-){3}
matches -
separated first 3 fields, (?:)
makes the group non-capturing
The captured group, ([^-]+)
will contain the -
separated 4th field.
While we are at this, you should perhaps look at string manipulation rather than costly Regex implementation, plain strings.Split()
should do:
package main
import (
"fmt"
"strings"
)
func main() {
s := "ab1-cde23-fg45-h6-ijk-789.lmn.local. 86400 IN A 12.34.5.123"
fmt.Println(strings.Split(s, "-")[3])
}