Re: regex, negations, grep, find and replace (a few questions)
- From: pk <pk@xxxxx>
- Date: Sun, 25 Nov 2007 19:25:29 +0100
(I'm changing some parts of the message due to spam filters on the server rejecting the message: substitute "**" with "tt", and "@@" with "a ")
jameshanley39@xxxxxxxxxxx wrote:
Here is a regex to match a link. Testing the regex using echo and
piping it
$ echo "abc<@@href=\"h**p://www.blah.com\">click here</a>" | grep -P
'<@@href="h**p://www.*>.*</a>'
a)
A problem is that grep works line by line. So
- it includes the abc before the link
Not if you use -o.
- if the pattern/link were broken over 2 lines, it would not match
(other tests suggest that)
I really don`t want it to include the abc at the beginning of the line
where the pattern matches.
Is there anything like grep that does not have this problem/feature ?
or where that problem/feature can be turned off?
Use -o (man grep). For the multi-line problem, just remove the newline characters before piping the text to grep, eg
echo $text | tr -d '\n' | grep 'your_pattern'
BTW, your pattern is not correct. First, it does not match links whose url does not start with "h**p://www". Second, due to sed "greedy" match behavior, if you had a text with more than one link, it would match from the beginning of the first link to the end of the last. Something like this would probably be a better pattern:
'<@@href="h**p://www[^>]*">[^<]*</a>'
b)
I heard that it is hard to negate a regex.
So, alternatives,
some programming languages provide advanced features to negate it.
Also, programs, like grep, allow you to negate it. (grep -v)
But grep does not let you do a replace. And On the negation of a
regex.
Which programs would , and how?
sed would be able to do this and much more else.
.
- Follow-Ups:
- Re: regex, negations, grep, find and replace (a few questions)
- From: jameshanley39@xxxxxxxxxxx
- Re: regex, negations, grep, find and replace (a few questions)
- References:
- regex, negations, grep, find and replace (a few questions)
- From: jameshanley39@xxxxxxxxxxx
- regex, negations, grep, find and replace (a few questions)
- Prev by Date: Re: Beagle chewing up CPU!
- Next by Date: Stats alt.os.linux (last 7 days)
- Previous by thread: regex, negations, grep, find and replace (a few questions)
- Next by thread: Re: regex, negations, grep, find and replace (a few questions)
- Index(es):
Relevant Pages
|