Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: A question about a regular expression

From: carl:cybercraft at: 17-Oct-2004 9:33

On Sunday, 17-October-2004 at 10:22:25 you wrote,
>Hello, >(first sorry for my poor English) >I've this string: "<a href='http://bla'>Test</a><a >href='http://blabla'>Test2</a>" >Now i would like to get Test and Test2. In Perl i use the following regexp: ><a href='.*?'>(.+?)</a> >So i get Test and Test2, but how can i do this in Rebol? >The user will specify the two string between are both Test and Test2, like: >Before: <a href='*'> >After: </a> >So i will get Test and Test2. > >Is there any way to do this?
Assuming the format of the string remains the same (and I've understood you correctly), you could use something like the following, (though it's a bit quick and dirty)...
>> str: "<a href='http://bla'>Test</a><a>href='http://blabla'>Test2</a>"
== {<a href='http://bla'>Test</a><a>href='http://blabla'>Test2</a>}
>> blk: parse/all str "<>"
== ["" "a href='http://bla'" "Test" "/a" "" "a" "href='http://blabla'" "Test2" "/a"]
>> blk/3
== "Test"
>> blk/8
== "Test2" This would work if your user supplied text with spaces in it too...
>> str: "<a href='http://bla'>Test One</a><a>href='http://blabla'>Test2</a>"
== {<a href='http://bla'>Test One</a><a>href='http://blabla'>Test2</a>}
>> blk: parse/all str "<>"
== ["" "a href='http://bla'" "Test One" "/a" "" "a" "href='http://blabla'" "Test2" "/a"]
>> blk/3
== "Test One"
>> blk/8
== "Test2" A proper parse rule to create a tidier output could be written of course, but if the above does what you want then you might as well use it. -- Carl Read