Jump to content

REGEXP, not at the beginning of string


yaba

Recommended Posts

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:

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

but it doesn't. It will match 'sss abc', but it won't match things like 'sss[b]abc[/b]sfg abc'.

Any help greatly appreciated :D
Link to comment
Share on other sites

[quote=yaba]
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!
[/quote]

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
[code]
SELECT
*
FROM
words
WHERE
wrd
NOT LIKE '{$chars}%'
AND wrd REGEXP('[[:<:]]{$chars}');
[/code]

[quote=yaba]
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 'sss[b]abc[/b]sfg abc'.
[/quote]

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
[code]
^[^{$chars}]
[/code]

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.