Reputation: 427
I am trying to match the 'words' that contain a specific string inside a provided string.
This reg_ex works great:
preg_match('/\b(\w*form\w*)\b/', $string, $matches);
So for example if my string contained: "Which person has reformed or performed" it returns reformed and performed.
However, I need to match codes inside codes so my definition of 'word' is based on splitting the string purely by a space.
For example, I have a string like:
Test MFC-123/Ben MFC/7474
And I need to match 'MFC' which should return 'MFC-123/Ben' and 'MFC/7474'.
How can I modify the above reg_ex to match all characters and use space as a boundary.
Thanks
Upvotes: 0
Views: 73
Reputation: 1314
If you want to get the whole block of text which does not contain space and contain your MFC as a match you can use the following regex:
\b(\S*MFC\S+)\b
explanation:
\b assert position at a word boundary (^\w|\w$|\W\w|\w\W)
1st Capturing group (\S*MFC\S+)
\S* match any non-white space character [^\r\n\t\f ]
Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed.
MFC matches the characters MFC literally (case sensitive)
\S+ match any non-white space character [^\r\n\t\f ]
Quantifier: Between one and unlimited times, as many times as possible, giving back as needed.
\b assert position at a word boundary (^\w|\w$|\W\w|\w\W)
example where matched blocks are in bold:
Test MFC-123/Ben jbas2/jda lmasdlmasd;mwrsMFCkmasd j2\13 MFC/7474
hope this helps.
Upvotes: 1
Reputation: 39443
Simply using this will do it for you:
(MFC\S+)
It means any non whitespace character after the MFC
If the MFC
comes in between text, or alone, then you can place \S*
before and after the MFC`. For example
(\S*MFC\S*)
This matches:
MFC-12312
1231-MFC
MFC
Upvotes: 2