简体   繁体   中英

Bash: sed regex pattern won't match strings

I have tested this particular regex in RegExr.com:

/(\*)*((\s)?(\w)*)/g

to match the following:

* Global Links contained...etc
* Change User, contact list...etc

(everything from ... on is just extra words in the sentence, not a literal ...etc)

I tried to use this regex in a sed command as part of a bash script like so:

sed "/(\*)*((\s)?(\w)*)/d" test.txt > stripped.txt

But these two lines still remain in stripped.txt. Is there something I'm not accounting for in the regex or in the file? before these two lines is the start of a block comment (/**) and the block comment end is after them(*/), both of these are on new lines. Am i missing something obscure with new lines or is the sed command/regex wrong?

You aren't accounting for the dialect of regex in use by sed by default. That's not a valid BRE (basic regular expression).

You need to tell sed to use ERE's (extended regular expressions).

For GNU sed that is the -r flag and for BSD sed that is the -E flag (though -r is often available as a compat flag).

sed -r "/(\*)*((\s)?(\w)*)/d" test.txt > stripped.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM