By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,678 Members | 1,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,678 IT Pros & Developers. It's quick & easy.

RegExp: How to match on exact string only?

P: n/a
I am trying to figure out how to set up my reg exp search so that the search
will only match on the exact word.

Here is the current problem code:

Word1 = "RealPlayer.exe"
Word2 = "Player.exe"

RegExp re = Word2;
if (re.Find(Word1))
{
bFound = TRUE;
}

Currently the bFound is set to TRUE since "Player.exe" is found within
"RealPlayer.exe". But I only want bFound to be TRUE is if the entire word
matches.

Can anyone assist with the correct syntax?

Many Thanks!

Mark

Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Lee
Mark Findlay said:

I am trying to figure out how to set up my reg exp search so that the search
will only match on the exact word.

Here is the current problem code:

Word1 = "RealPlayer.exe"
Word2 = "Player.exe"

RegExp re = Word2;
if (re.Find(Word1))
{
bFound = TRUE;
}

Currently the bFound is set to TRUE since "Player.exe" is found within
"RealPlayer.exe". But I only want bFound to be TRUE is if the entire word
matches.

Can anyone assist with the correct syntax?


The correct syntax is:

if(Word1 == Word2)
{
bFound = true;
}
It's a waste of processor time to use regexp's for an exact match.

Jul 23 '05 #2

P: n/a
The sample us a bit simplified - the values being compared may or may not be
reg expressions so we have to assume that they will be. With this
assumption, can you recommend a regexp syntax that will match on the exact
word only?

Thanks,
Mark

"Lee" <RE**************@cox.net> wrote in message
news:c8*********@drn.newsguy.com...
Mark Findlay said:

I am trying to figure out how to set up my reg exp search so that the searchwill only match on the exact word.

Here is the current problem code:

Word1 = "RealPlayer.exe"
Word2 = "Player.exe"

RegExp re = Word2;
if (re.Find(Word1))
{
bFound = TRUE;
}

Currently the bFound is set to TRUE since "Player.exe" is found within
"RealPlayer.exe". But I only want bFound to be TRUE is if the entire word
matches.

Can anyone assist with the correct syntax?


The correct syntax is:

if(Word1 == Word2)
{
bFound = true;
}
It's a waste of processor time to use regexp's for an exact match.

Jul 23 '05 #3

P: n/a
Lee
Mark Findlay said:
>Here is the current problem code:
>
>Word1 = "RealPlayer.exe"
>Word2 = "Player.exe"
>
>RegExp re = Word2;
>if (re.Find(Word1))
>{
> bFound = TRUE;
>}
The sample us a bit simplified - the values being compared may or may not be
reg expressions so we have to assume that they will be. With this
assumption, can you recommend a regexp syntax that will match on the exact
word only?

One other thing that should be clarified is that your
example code is not Javascript. Javascript doesn't
have type declarations, is case-sensitive, and the
RegExp object doesn't have a "find" method. Make sure
you're working in the correct language.

The solution to your problem seems to be to tie the
provided regular expression to the beginning and end
of the matching string:

Word1 = "RealPlayer.exe"
Word2 = "Player.exe"

re = new RegExp("^"+Word2+"$");
bFound = re.test(Word1);

Jul 23 '05 #4

P: n/a
Mark Findlay wrote:
I am trying to figure out how to set up my reg exp search so that the search
will only match on the exact word.

Here is the current problem code:

Word1 = "RealPlayer.exe"
Word2 = "Player.exe"

RegExp re = Word2;
if (re.Find(Word1))
{
bFound = TRUE;
}

Currently the bFound is set to TRUE since "Player.exe" is found within
"RealPlayer.exe". But I only want bFound to be TRUE is if the entire word
matches.

Can anyone assist with the correct syntax?

Many Thanks!

Mark


Word1 = "^Player.exe"; // "^" denotes it must match from the start of line
// or
Word1 = "^Player.exe$";
// "^" denotes it must match from the start of line
// "$" denotes it must match the end of line, in other words, an exact match

of course, as has been already pointed out, a regex match against "^...$" is the
same as testing if the two strings are equal.

Some regex references:

<url:
http://devedge.netscape.com/library/...ce/frames.html
/>
<url:
http://msdn.microsoft.com/library/en...asp?frame=true
/>

I prefer the Netscape one, because, although older, everything mentioned there
works in newer browsers. Whereas some of the newer features to Javascript (such
as "?" non-greedy and "(?:pattern)" non-capturing match) only work in the most
modern browsers.

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #5

P: n/a
Grant Wagner <gw*****@agricoreunited.com> writes:
Word1 = "RealPlayer.exe"
Word2 = "Player.exe"
of course, as has been already pointed out, a regex match against
"^...$" is the same as testing if the two strings are equal.


.... except when the string contains characters that are meaningfull in
RegExps. In this case RegExp("^Player.exe$") would also match
"Playerfexe". :)

Apart from that nitpick, I agree.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #6

P: n/a
"Mark Findlay" <mf******@speakeasy.org> wrote:
I am trying to figure out how to set up my reg exp search so that the search
will only match on the exact word.

Here is the current problem code:

Word1 = "RealPlayer.exe"
Word2 = "Player.exe"

RegExp re = Word2;
if (re.Find(Word1))
{
bFound = TRUE;
}

Currently the bFound is set to TRUE since "Player.exe" is found within
"RealPlayer.exe". But I only want bFound to be TRUE is if the entire word
matches.

Can anyone assist with the correct syntax?


var re = /\bPlayer.exe\b/

http://msdn.microsoft.com/library/en...gexpsyntax.asp

\b
Matches a word boundary, that is, the position between a word and a
space. For example, 'er\b' matches the 'er' in "never" but not the
'er' in "verb".

Regards,
Steve
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.