...

View Full Version : Resolved PHP Regex Help



Rowsdower!
01-22-2010, 05:00 PM
I've never written a successful regex in my life and I'm starting to tinker with them now. I have a text file, the contents of which I am loading into a variable. The basic text file looks like a lot of lines like this:

"PRBUPort"="80"
"PRBUPath"="/"
"PRBUUrl"="http://mysite.com/"

I then want to run through the variable and pull out all instances of "PRBUUrl"="<some url here>" so that I can capture all URLs passed in the text file for this particular recurring variable.

I'm using this preg_match_all to capture each instance:

preg_match_all('%"PRBUUrl"=".*"%',$text_1,$url_list_1);

That's working well enough, but I'd really like to actually exclude the "PRBUUrl"=" and " parts so that I get ONLY the part in the middle. The URL is not always an actual URL - it can have wildcard characters (*), IP addresses, etc. and other URLs actually appear in other places that I DON'T want to capture (so I can't just capture all URLs, for example).

This is supposed to be pretty easy stuff but it's just not clicking for me... Any ideas?

JAY6390
01-22-2010, 05:23 PM
preg_match_all('%"PRBUUrl"="\K[^"]+%',$text_1,$url_list_1);
Try that

abduraooft
01-22-2010, 05:26 PM
preg_match_all('%"PRBUUrl"="\K[^"]+%',$text_1,$url_list_1);
Try that
:eek: are you imitating the
][/COLOR]-BBCode's color pattern?

JAY6390
01-22-2010, 05:29 PM
lol I just copied and pasted the line, and edited the regex directly...the forum textbox kept the formatting for me...

abduraooft
01-22-2010, 05:32 PM
lol I just copied and pasted the line, and edited the regex directly...the forum textbox kept the formatting for me... Oops. I use the other "Mode" of the editor, so I thought ... :o

Feel bad to hijack the thread anymore

Also, if you don't want the forum to parse tags, you can just use the [noparse] tags around tags, and they'll stay intact :thumbsup: no need to put fonts inside them :cool:

I know, but that's a different story (http://codingforums.com/showpost.php?p=763882&postcount=8) :)

JAY6390
01-22-2010, 05:34 PM
I quite like the full version, especially the and
buttons. Most other forums don't have this :)

JAY6390
01-22-2010, 05:36 PM
Also, if you don't want the forum to parse tags, you can just use the [noparse] tags around tags, and they'll stay intact :thumbsup: no need to put fonts inside them :cool:

Rowsdower!
01-22-2010, 05:54 PM
preg_match_all('%"PRBUUrl"="\K[^"]+%',$text_1,$url_list_1);
Try that

Make it look easy, why don't you?!

That worked perfectly, thanks! I need to spend some time studying the syntax because I was trying tons of stuff, but nothing even close to that. :(

JAY6390
01-22-2010, 06:01 PM
The syntax is very simple. Up until the \K it matches the text as you know. then the \K makes it forget everything before it (ie the text "PRBUUrl"="). Then the [^"]+ matches all the characters that follow that aren't the character " so once it reaches the closing " it stops matching and hey presto, you have the match perfect

I've been using regex for years now and absolutely love working out regex's so play around with them constantly, so yes it comes pretty easy now. It wasn't at the beginning. Practice makes perfect as they say!
:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum