Forum Moderators: coopster

Message Too Old, No Replies

Regexp question

         

DrDoc

8:38 pm on Apr 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Say that I have this string:
foo bar widget class house rice mouse

I want to add an "s" to the end of each word, unless the word is "mouse" or ends with "ce" or "ss".

How do I match which words should get the "s"?

brotherhood of LAN

8:44 pm on Apr 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



searching for "porter stemmer" may give you good direction on it, though I don't think there's a stemmer that is "perfect" for any language.

[zend.com...]
which links to this place
[snowball.tartarus.org...]

there's a script at the bottom, might be worthwhile!

DrDoc

8:58 pm on Apr 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for your help... However, I should have been more clear :)

The example has nothing to do with what I need. I have several instances of where this type of regexp needs to be implemented. I was just trying to give a general description of the question behind the regexp...

So, ignoring the language aspects...

Say that I have this string instead:
foo bar widget blah blouh bleh bla

I want to add "et" to the end of each word, unless the word already ends with "et" or is "foo" or "bar".

How do I match which words should get the "et"?

DrDoc

9:07 pm on Apr 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Would something like this work?

preg_replace("/[^(foo¦bar¦et)]$/","et",$string);

brotherhood of LAN

9:11 pm on Apr 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I want to add "et" to the end of each word, unless the word already ends with "et" or is "foo" or "bar".

substr can be used to match the ending characters, or a preg_match/replace maybe?

1) iterate through the words
2) if (substr($word,-2,2)!= 'et'&$word!= 'foo'&$word!= 'bar')
{ $word .= 'et'; }

too slow...

DrDoc

9:19 pm on Apr 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, that's true... Your example would work very well!

My own preg_replace doesn't work since it replaces whatever the last character is with "et"... (or so I assume, since I haven't tested it yet)

If possible, I'd like to do it with a preg_replace. That way I can do it on long strings. I'm just not sure what the actual preg_replace would look like :(

brotherhood of LAN

9:32 pm on Apr 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if(!preg_match("'^(foo¦bar)$'",$string)&preg_match("'et$'",$string))
{
$string .= 'et';
}

I guess if you need to make sure that its not "foo" or "bar" and need to make sure the word doesnt end in 'et' there's not much point of doing a preg_replace. So instead you just put the 'et' in at the end of the string. I'm no PHP expert though ;o)