If you want to match a prefix in a string and stop at the n'th occurence of a delimiter in that string then you need non-greedy matching. This is how you would do it if the regex engine you are using doesn't support it natively, do it by negative character class.
Example: given a URI http://service.domain.tld/path1/path2/path3
and you only want service.domain.tld
by splitting on /
, then:
$ echo "http://service.domain.tld/path1/path2/path3" | sed 's@http://\([^/]*\).*@\1@' service.domain.tldNow what happens if you need the suffix after the delimiter? Just change the capturing parens:
$ echo "foo_bar_baz" | sed 's/[^_]*_\(.*\)/\1/' bar_bazCourtesy of: http://stackoverflow.com/questions/1103149/non-greedy-regex-matching-in-sed
No comments:
Post a Comment