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
https://forums.phpfreaks.com/topic/20589-regexp-not-at-the-beginning-of-string/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.