You are not logged in.
echo 'anything<h1>something</h1>anything<h1>somethingelse</h1>anything' | perl -pe 's/.*\<h1\>(.*)\<\/h1\>.*/\1/g'
somethingelse
i want "something" (i.e. the first match left-to-right). is there a regex that will accomplish this?
the end goal is use in php's preg_replace function, but it's easier to test/play with using the above.
Last edited by brisbin33 (2010-06-03 14:24:24)
//github/
Offline
Oops, was too quick to respond. I don't think I understand the question. Let me ponder for a while...
Last edited by Trent (2010-06-03 02:51:52)
Offline
Why are you parsing html with regex? Don't do that!
[git] | [AURpkgs] | [arch-games]
Offline
You want a non-greedy qualifier (?) on the first .* And the /g is unnecessary if you want only the first match.
s/.*?\<h1\>(.*)\<\/h1\>.*/\1/
Offline
This works, but Daenyth is right here..
echo 'anything<h1>something</h1>anything<h1>somethingelse</h1>anything' | perl -pe 's/.*?\<h1\>(.*?)\<\/h1\>.*/\1/'
archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson
Offline
@Trent, i could only get it to work by adding a second ? inside the ( )
@rson, nicely done, see above .
@Daenyth, i'll be looking into DOMs and whatnot, but in my simple situation, this regex will work well.
Last edited by brisbin33 (2010-06-03 14:09:18)
//github/
Offline
Yep, that's right. I think I assumed it was already non-greedy. Despite the obvious lack of qualifier. I have no excuse.
Offline
just as additional info for those that want it. i looked into PHP5's out of the box DOM stuff. it's quite easy to use. but you can't extract from commented tags (which i need).
http://simplehtmldom.sourceforge.net/manual.htm is a sweet drop-in script that works really well (i'll be using it in general going forward, Daenyth ).
i found it b/c their manual specifically mentions a special feature where $html->find('comment') will return all comments, within which i could then $comment->find('my custom tag').
sadly, it doesn't work. no output. i made a simple test script and emailed the writer of this software, we'll see if i'm dumb or it needs fixing.
cheers!
//github/
Offline