Dannyboy
Dannyboy

Reputation: 2052

trying to run regex against semi-colon separated list of words

I have a semicolon-separated list of words (about 180,000 words), like so:

;test;dog;cat;hello;whatever;

How would I get an aray of all words that contains specific substring using regex? For example - how would I get all words hat contain letter a? I've tried this:

preg_match_all('#(?:;).*a.*(?:;)#u', $one_line, $matches);

^But it doesn't seem to produce correct results?... what am I doing wrong?

Upvotes: 1

Views: 941

Answers (3)

Tai
Tai

Reputation: 1256

Braj is correct for the regex example however I wanted to bring up another way to do this might be to split the string by the semicolons.

Ex use something like this:

array explode ( string $delimiter , string $string [, int $limit ] )

-Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.

then you can simply check if each string contains the letter a. I believe that in some cases this may be more efficient. A case where it might be more efficient is if you are looking for words containing 'a','aa', and 'aaa'. You can split the strings into an array then check each string for containing an 'a'. Then you can shorten the strings you need to check for 'aa' and 'aaa'. Just thought I would point out that assuming regex is necessary is not necessarily going to always be beneficial or most helpful

Upvotes: 0

Braj
Braj

Reputation: 46841

get all words hat contain letter a?

get the matched group from index 1 using preg_match_all method.

(\w*a\w*)

Online demo


If there are multiple words between semicolon then try below regex as suggested by @ anubhava in below comments.

[^;a]*a[^;]*

Online demo

Upvotes: 6

fquinner
fquinner

Reputation: 538

Personally I would tend to two-step this... first, explode the string into an array:

$entries = explode (";", $yourstring);

Then you can do a clean regex on each component in the array without having to worry about the semi colon:

$matches = preg_grep ("/a/" , $entries);

Trying to include the delimiter along with the other regex in my experience can make things pretty complicated pretty quickly.

Upvotes: 2

Related Questions