Need some help with a regular expression that I cant seem to work on. Say there is the following text -
Input
We all (need some) help some day (ps: when it rains) and I need to save some hay now.
During the rainy season (ps: when it might rain heavily) Seattle gets drenched but not (all of pacific) northwest.
one of the best things that could (ps: happen) with any one is winning a lottery(1 atlas)
Output
We all (need some) help some day and I need to save some hay now.
During the rainy season Seattle gets drenched but not (all of pacific) northwest.
one of the best things that could with any one is winning a lottery(1 atlas)
I only want to eliminate anything in parenthesis as long as it starts with ps:
here is what i did
Opera $cat text | sed 's/(ps:*.*)//g'
We all (need some) help some day and I need to save some hay now.
During the rainy season northwest.
one of the best things that could
...as you can see that is not working when there is any parenthesis after where it finds the ps:.
It's a bit tricky because what seems a logical approach, /(ps:.*)/ , assumes a non-greedy match. Regex, however, is a greedy match and that expression will match up through the last close paren on the line. Something like this:
sed 's/(ps:[^)]*)//g' input-file # no need to cat, sed can read on it's own
will match (ps: followed by anything that isn't a paren (zero or more times) followed by a paren. This, is a non-greedy match and so for a line like:
here is (remains) my example (ps: removed) of input (remains too).
[root@hostname test]# perl -pe 's/\(ps:.*?\) //g' input
We all (need some) help some day and I need to save some hay now.
During the rainy season Seattle gets drenched but not (all of pacific) northwest.
one of the best things that could with any one is winning a lottery(1 atlas)