Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 671
user1892775 Avatar asked Sep 11 '25 09:09

user1892775


1 Answers

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")
like image 106
user10753492 Avatar answered Sep 13 '25 05:09

user10753492