return text inbetween...

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

Moderator: General Moderators

Post Reply
Tsunexus
Forum Newbie
Posts: 1
Joined: Wed Jul 19, 2006 2:23 pm

return text inbetween...

Post by Tsunexus »

is there a way that when given a string, it will search the string and return only what is in between lets say '[bob]' and '[/bob]'
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

regex

EDIT: Oops... I thought this was posted in php code... sorry! :oops:
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Re: return text inbetween...

Post by Buddha443556 »

Tsunexus wrote:is there a way that when given a string, it will search the string and return only what is in between lets say '[bob]' and '[/bob]'
Regular expression (Perl) might look something like this:

Code: Select all

/\[bob\](.*?)\[\/bob\]/is
Also see preg_match().
dan.kotowski
Forum Newbie
Posts: 8
Joined: Thu Jul 20, 2006 10:22 pm

Post by dan.kotowski »

I have a dictionary file that has entries like this:

Code: Select all

<o:ent xmlns:o="http://oup.dataformat.com/doc/OUP_DTD_Dictionary.html" sortkey="aalborg"><o:hwGrp><o:hw>Aal<o:hsb/>borg</o:hw><o:pronGrp><o:pr type="US"> |Àà&#8730;¥lÀåb&#8730;¥r(g)|</o:pr><o:pr type="US_IPA"> |Àå…ëlÀàb…îrg|</o:pr><o:pr type="US_IPA"> |Àå…îlÀàb…îrg|</o:pr><o:pr type="UK_IPA"> |Àå…îÀêlb…îÀêg|</o:pr></o:pronGrp><o:varGrp> (also<o:v> &#8730;Öl<o:hsb/>borg</o:v>) </o:varGrp></o:hwGrp><o:SB><o:sense> <o:def> an industrial city and port in northern Jutland, Denmark; pop. 155,000. </o:def></o:sense></o:SB></o:ent>
How would I get just the part after the sortkey, so that they would end up like this:

Code: Select all

aalborg
???
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Code: Select all

$pattern = "/sortkey=\"(.*?)\"/i";
dan.kotowski
Forum Newbie
Posts: 8
Joined: Thu Jul 20, 2006 10:22 pm

Post by dan.kotowski »

I've got this:

Code: Select all

<?php
$subject = <text from file>;
$pattern = "/sortkey=\"(.*?)\"/i";
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>
But I need it to read a file that contains an entire dictionary. How would I set that up to just give me the sortlist part?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Post Reply