首页 > 解决方案 > Grep not working with complex regular expression

问题描述

The goal is to use grep and find all URL paths containing an underscore. Underscores in the query string are ignored. Examples:

Positives:
/abc_bcd/def/
/foo_bar_foo
/image_bar?s=color
/foo_bar
/foo_remover?s=foo_bar

Negatives:
/foo
/foo/bar/foo
/foo/bar/foo?s=foo_bar
/foo-supersizer
/foosupersizer
/foobar
/foo-supersizer?s=foo_bar
foo_bar
foo bar bar_foo
foo_bar_foo

This regular expression works, but applying it inside of grep (on macOS) fails to yield any files even though there are ones containing matching paths.

Regular expression: /^(?=[^?\s]*_)(?:\/[-a-zA-Z0-9()@:%_?\+.~#&=]+)+\/?$/gm

RegEx test: https://regex101.com/r/tIYoP7/3

Grep command: grep -r "^(?=[^?\s]*_)(?:\/[-a-zA-Z0-9()@:%_?\+.~#&=]+)+\/?$" .

Does grep require special formatting for regular expressions on macOS?

标签: macosterminalgrep

解决方案


Given the sample input/output you posted all you need is:

$ grep '^/[^?]*_' file
/abc_bcd/def/
/foo_bar_foo
/image_bar?s=color
/foo_bar
/foo_remover?s=foo_bar

If that isn't all you need then edit your question to provide more truly representative sample input and expected output that includes cases where the above doesn't work.


推荐阅读