I have a use case: Need to match the following . Input string: abpcdcccddd Matching criteria: match all characters starting with 'a' and ending with 'ccc' Example: abpcdccc ( single character 'c' in 4th Poistion got successfully ignored in matching)
Can you help me with the Golang regular expression for the same?
I'm not sure that I understand exactly what it is you are wanting, but I can probably get you on the right track.
If you want to match a string that has an a
, followed by some word characters, followed by ccc
, then you can simply use something like this:
a\w+ccc
If you want the string to start and end with a
and ccc
respectively, you can do something like this:
^a\w+ccc$
If you want to only allow specific characters like lowercase letters, you can put them into a character class like this:
a[a-z]+ccc
Hopefully one of those answers your question.
It all depends on what do you exactly mean with "all characters"... it could include or not character sets such as whitespace (optionally including newlines), letters, numbers, symbols, unicode sets, etc.
So...
If you really meant "all characters" (i.e. including whitespace) between the first "a" and "ccc" you need to use:
a.+ccc
If you meant just all "word characters" you can use one of these:
a\w+ccc
a[0-9a-zA-Z_]+ccc
All letters:
a[a-zA-Z]+ccc
All lowcase letters:
a[a-z]+ccc
...and there will be more options depending on your real requirements for that definition.
Here you have a small program to test some of that cases:
package main
import "fmt"
import "regexp"
func main() {
res := []string{
`a.+ccc`,
`a\w+ccc`,
`a[a-z]+ccc`,
}
ss := []string{
"ablablacccc",
"adadasdccc",
"sadlkasdlkcccc",
"accc",
"blaafoewoiccc",
"oirorwccc",
"abla ablaccc",
}
for _, re := range res {
r := regexp.MustCompile(re)
fmt.Printf("regular expression: %q
", re)
for _, s := range ss {
fmt.Printf(" case %q: matches=%v match=%q
", s, r.MatchString(s), r.FindString(s))
}
}
}
And the output it produces:
regular expression: "a.+ccc"
case "ablablacccc": matches=true match="ablablacccc"
case "adadasdccc": matches=true match="adadasdccc"
case "sadlkasdlkcccc": matches=true match="adlkasdlkcccc"
case "accc": matches=false match=""
case "blaafoewoiccc": matches=true match="aafoewoiccc"
case "oirorwccc": matches=false match=""
case "abla ablaccc": matches=true match="abla ablaccc"
regular expression: "a\\w+ccc"
case "ablablacccc": matches=true match="ablablacccc"
case "adadasdccc": matches=true match="adadasdccc"
case "sadlkasdlkcccc": matches=true match="adlkasdlkcccc"
case "accc": matches=false match=""
case "blaafoewoiccc": matches=true match="aafoewoiccc"
case "oirorwccc": matches=false match=""
case "abla ablaccc": matches=true match="ablaccc"
regular expression: "a[a-z]+ccc"
case "ablablacccc": matches=true match="ablablacccc"
case "adadasdccc": matches=true match="adadasdccc"
case "sadlkasdlkcccc": matches=true match="adlkasdlkcccc"
case "accc": matches=false match=""
case "blaafoewoiccc": matches=true match="aafoewoiccc"
case "oirorwccc": matches=false match=""
case "abla ablaccc": matches=true match="ablaccc"