Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regexp question

Status
Not open for further replies.

skiflyer

Programmer
Sep 24, 2002
2,213
US
Ok.... this isn't a PHP specific question, but since it's the PHP implementation I'm concerned about this was the best place I could think to post...

I want to find the word version as many times as it appears. Simple as that, right?

Code:
$pattern = '/\W+(Version)\W+/';
$subject = 'Abc Version Def';

Works just fine, as expected...
Code:
$subject = 'Version';
Has no matches, as expected....

So I'm scratching my head on how to get the word version appropriately without being thwarted by BOF and EOF occurences. (Because VersionString should fail this match is the reason for the \W's btw)

Anyway, I know this isn't hard, but I'm temporarily stumped and would appreciate some nudges
 
I'd like to help, however, I don't follow (sorry).
Could you post an example of what a real subject looks like and of the needle?
I'm a bit slow today, so I don't get the BOF/EOF thing either. Please help me understand.
 
BOF = Beginning of file, EOF = End of file

and the answer is
Code:
$pattern = '/\bVersion\b/';

\b is a word boundary, I was mistakingly try to use any non-word character as a word boundary, but that causes these BOF & EOF hangups.
 
Yep. I was wondering why you'd use \W. Also, I know what BOF/EOF is, but it wasn't clear to me in what context - you actually meant that when the version appears at the beginning or the end of the file the regex failed. Right?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top