Anyone good at "regular expressions"?
#1
Anyone good at "regular expressions"?
If so...
how do you match a whole word from a string.
eg. word = "test"
String = "test, test testing....test;"
I need it to match all of those apart from the "test" part of "testing".
Thanks in advance
Simon
how do you match a whole word from a string.
eg. word = "test"
String = "test, test testing....test;"
I need it to match all of those apart from the "test" part of "testing".
Thanks in advance
Simon
#6
Scooby Regular
Join Date: Jul 2002
Location: West Sussex
Posts: 271
Likes: 0
Received 0 Likes
on
0 Posts
Trending Topics
#10
I think the 'regexp' (purist!??) way would be to use a character class eg {., }word{., } which will match any of the supplied options (rather than the /b); regexp being language independant.
That said, if php supplies a /b style option, use it, it's a lot easier the generating a complex regexp!
That said, if php supplies a /b style option, use it, it's a lot easier the generating a complex regexp!
#11
Scooby Regular
Character classes are done with square brackets rather than curly braces, which are used for matching repetition. The regex purist will use what's available in the implementation, rather than sticking to one way of doing things If the PHP regular expression parser is POSIX compliant it should be ok.
#12
OK..
it gets more complicated....
I'm using the \b thing which works perfectly... thank you...
But.. I want it to ignore the text if it is within certain specific tags...
best example would be in html...
testing test <a href="http://www.test.com">test</a> test <img src="http://www.test.com/image.jpg">
in this example, I would want it to ignore the two "test"s between "<a href" and "</a>" and also ignore the test inbetween "<img" and ">"
is this possible, or does it get way too complex?
Cheers
Simon
it gets more complicated....
I'm using the \b thing which works perfectly... thank you...
But.. I want it to ignore the text if it is within certain specific tags...
best example would be in html...
testing test <a href="http://www.test.com">test</a> test <img src="http://www.test.com/image.jpg">
in this example, I would want it to ignore the two "test"s between "<a href" and "</a>" and also ignore the test inbetween "<img" and ">"
is this possible, or does it get way too complex?
Cheers
Simon
#15
Scooby Regular
Join Date: Jul 2000
Location: Islington
Posts: 2,145
Likes: 0
Received 0 Likes
on
0 Posts
how about a routine that scans the page and replaces everything between "<a href" and "/a>" with spaces. same with "<img" tags and then do your /b thingie?
kinda like a mid(rah, rah, rah)=space$(1) in vb
kinda like a mid(rah, rah, rah)=space$(1) in vb
#16
Was trying to use a regular expression to avoid parsing all the text each time for performance.
If it can't be done in one regular expression, I guess I'll have to run two processes on it.
Cheers
Simon
If it can't be done in one regular expression, I guess I'll have to run two processes on it.
Cheers
Simon
#17
Scooby Regular
Regex matching is anything but efficient so you're already imposing a performance hit using them, even if you precompile it. You can't do what you want with one regular expression, you'd be better off with a mixture of some kind of SAX parser (so you can ignore all the HTML tags) and regex the substitution.
Hmm, saying that, I just did this:
my $word_to_replace = 'test';
my $new_word = 'april fools day';
$html =~ s!(>[^<]*?\b)$word_to_replace\b!$new_word!gi;
Hmm, saying that, I just did this:
my $word_to_replace = 'test';
my $new_word = 'april fools day';
$html =~ s!(>[^<]*?\b)$word_to_replace\b!$new_word!gi;
Last edited by stevencotton; 01 April 2004 at 02:26 PM.
Thread
Thread Starter
Forum
Replies
Last Post
Mattybr5@MB Developments
Full Cars Breaking For Spares
33
29 August 2017 07:18 PM
Sambob
Engine Management and ECU Remapping
41
27 November 2015 07:36 PM
Mattybr5@MB Developments
Full Cars Breaking For Spares
20
22 October 2015 06:12 AM