Regex to deny sentence

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
magaupe
Forum Newbie
Posts: 6
Joined: Thu Sep 06, 2012 10:08 am

Regex to deny sentence

Post by magaupe »

Hi guys,

I need to find any URL (in red) inside this sentence:

<link rel="image_src" href="any URL"

But it cannot be at any circunstante this one:

<link rel="image_src" href="http://s1. trrsf. com .br/atm/3/core/_img/terra-logo-white-bg-v2 .jpg"

I'm using this regex but it finds everything:

<link rel="image_src" href="([^\"]+)"

Any advice?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Regex to deny sentence

Post by requinix »

So... if the href you matched was that one you don't want, skip it.
magaupe
Forum Newbie
Posts: 6
Joined: Thu Sep 06, 2012 10:08 am

Re: Regex to deny sentence

Post by magaupe »

requinix wrote:So... if the href you matched was that one you don't want, skip it.
exactly.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Regex to deny sentence

Post by requinix »

So what's the question? Or what code do you have?
magaupe
Forum Newbie
Posts: 6
Joined: Thu Sep 06, 2012 10:08 am

Re: Regex to deny sentence

Post by magaupe »

requinix wrote:So what's the question? Or what code do you have?
I have this code but it matchs any href without distiction.
<link rel="image_src" href="([^\"]+)"

it should not match the URL: http://s1. trrsf. com .br/atm/3/core/_img/terra-logo-white-bg-v2 .jpg.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Regex to deny sentence

Post by requinix »

And I'm telling you the easiest option: let it match whatever it wants to match and make the rest of the code skip the match if you don't want it.

Code: Select all

for each $href in all the hrefs the regex matched {
    if $href is something you don't want to include {
        continue looking at the next href
    } otherwise {
        do whatever
    }
}
There may be a perfectly legitimate reason why that won't work for your circumstance but I haven't heard it yet.
magaupe
Forum Newbie
Posts: 6
Joined: Thu Sep 06, 2012 10:08 am

Re: Regex to deny sentence

Post by magaupe »

requinix wrote:And I'm telling you the easiest option: let it match whatever it wants to match and make the rest of the code skip the match if you don't want it.

Code: Select all

for each $href in all the hrefs the regex matched {
    if $href is something you don't want to include {
        continue looking at the next href
    } otherwise {
        do whatever
    }
}
There may be a perfectly legitimate reason why that won't work for your circumstance but I haven't heard it yet.
I'd like to do this in a single regex line. Not using any kind of iteration.
Is that possible?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Regex to deny sentence

Post by requinix »

Yeah, but aren't you going to need iteration somewhere? What are you doing with these hrefs?

Code: Select all

<link rel="image_src" href="(?!url you don't want)([^\"]+)"
Remember to escape characters like . and /.
magaupe
Forum Newbie
Posts: 6
Joined: Thu Sep 06, 2012 10:08 am

Re: Regex to deny sentence

Post by magaupe »

requinix wrote:Yeah, but aren't you going to need iteration somewhere? What are you doing with these hrefs?

Code: Select all

<link rel="image_src" href="(?!url you don't want)([^\"]+)"
Remember to escape characters like . and /.
Not really, I just need this to extract a image from a web page.
Is this right? It won't recognize some of the characteres.

Code: Select all

<link rel="image_src" href="(?!http://s1.trrsf.com.br/atm/3/core/_img/terra-logo-white-bg-v2.jpg)([^\"]+)"
I'm using Testrexp to test it. This is the error: "TRegExpr(comp): Urecongnized Modifier (pos 96)"
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Regex to deny sentence

Post by requinix »

Where is offset 96?

Also,
requinix wrote:Remember to escape characters like . and /.
magaupe
Forum Newbie
Posts: 6
Joined: Thu Sep 06, 2012 10:08 am

Re: Regex to deny sentence

Post by magaupe »

Actually It must be compatible cause the application I'm using it is based on TestRExp (http:// regexpstudio. com/RegExpStudio.html).
Thanks anyway.
Post Reply