Jump to content


Photo

REGEXP, not at the beginning of string


  • Please log in to reply
2 replies to this topic

#1 yaba

yaba
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 13 September 2006 - 03:13 AM

Hello,

I need a REGEXP that would match a phrase that contains a word starting with some characters, but would not match a phrase that starts with a word, starting with those chars!

???

Here's an example:

search chars = 'abc'

'sss abcsss' (match)
'sss abc abcsss' (match)
'abc sss abcsss' (no match)

I thought this (when calling from PHP) would work:

"SELECT * FROM words where wrd REGEXP '^[^({$chars})].*[[:<:]]{$chars}' ORDER BY wrd ASC";

but it doesn't. It will match 'sss abc', but it won't match things like 'sssabcsfg abc'.

Any help greatly appreciated :D

#2 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 13 September 2006 - 10:35 AM

I need a REGEXP that would match a phrase that contains a word starting with some characters, but would not match a phrase that starts with a word, starting with those chars!


If by [not]starting with some characters you mean a string, with characters in a specific order then the following should be what you're looking for
SELECT
*
FROM
words
WHERE
wrd
NOT LIKE '{$chars}%'
AND wrd REGEXP('[[:<:]]{$chars}');

thought this (when calling from PHP) would work:

"SELECT * FROM words where wrd REGEXP '^[^({$chars})].*[[:<:]]{$chars}' ORDER BY wrd ASC";
but it doesn't. It will match 'sss abc', but it won't match things like 'sssabcsfg abc'.


You stated that the characters should begin a word, not be in any part of the word. So I don't understand this comment.

The regexp that you posted will work if you're saying that the word shouldn't start with any of the characters in the string.

eg:
if $chars = 'abc'
then the regexp won't match strings like the following
'a abc foo'
'b abcd bar'
'c oeirr'

The only error would be the inclusion of the brackets (). It should be
^[^{$chars}]

I assume that you "misspoke" when you said that the phrase should not start with any of the characters and instead meant that the phrase should not start with the string. In which case the query I posted should work.

#3 yaba

yaba
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 13 September 2006 - 03:48 PM

Yup, that did it! Thanks ;)

I indeed got confused trying to express myself!  ???




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users