user1892775
user1892775

Reputation: 2111

How to match string that does not contain a specific string

I would like to write regexp in Go to match a string only if it does not contain a specific substring (-numinput) and contain another specific string (-setup).

Example, for inputStr

The following type of strings should NOT match because -numinput is present

str = "axxx yy  -setup  abc -numinput 12345678 aaa"

The following type of strings should match as -setup is present and -numinput is not present

str = "axxx yy  -setup  abc aaa"

The following type of strings should not match because -setup is not present even though -numinput is not present

str = "axxx yy abc aaa"

I came across some posts like Regular expression to match a line that doesn't contain a word?

But, I just dont understand how to do it in Golang

Upvotes: 4

Views: 9847

Answers (1)

user10753492
user10753492

Reputation: 778

If you want to parse command line flags, consider using the flag package

https://golang.org/pkg/flag/

For general string related functionality, considers the strings package

https://golang.org/pkg/strings/

In your case:

strings.Contains(str, "-setup") && !strings.Contains(str, "-numinput")

Upvotes: 4

Related Questions