Re: regex, negations, grep, find and replace (a few questions)



(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.
.



Relevant Pages

  • regex, negations, grep, find and replace (a few questions)
    ... Here is a regex to match a link. ... A problem is that grep works line by line. ... Is there anything like grep that does not have this problem/feature? ... I heard that it is hard to negate a regex. ...
    (alt.os.linux)
  • Re: A newbie question about grep & find
    ... By piping the output of find through to grep, ... I prefer piping commands and using the few switches that I ...
    (comp.os.linux.misc)
  • Re: Basic Grep Question
    ... Could someone please tell me how I can grep for two or more different ... words in one command instead of piping them through. ... Lucha Contra La Pobreza - PADEP ...
    (Fedora)
  • Re: regular expression question
    ... match lines that do not contain the chars "sync". ... you would negate the test: ... but that won't work as an option for grep. ... the word "sync". ...
    (comp.lang.perl.misc)
  • Re: OT: Which tool, and how, to get partial string from file?
    ... >>some switch to grep, or perhaps piping the results to sed or awk, but ... >>I'm unfamiliar with these tools, and an hour of trying to grok the man ...
    (Debian-User)