473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

RegExp: How to match on exact string only?

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
6 56789
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
6
by: Nick | last post by:
Hi, How can I check if a number exists by itself in this string by using the RegExp object? --- var mystring = "11,111,01,011"; var match = "1"; var re = new RegExp( match );
3
by: jasonkester | last post by:
Just a heads up for anybody that comes across this in the future. Noticed a strange behavior in RegExp.test() today. Check out the following code. It will alternately display "chokes" and null,...
8
by: Dmitry Korolyov | last post by:
ASP.NET app using c# and framework version 1.1.4322.573 on a IIS 6.0 web server. A single-line asp:textbox control and regexp validator attached to it. ^\d+$ expression does match an empty...
11
by: HopfZ | last post by:
I coudn't understand some behavior of RegExp.test function. Example html code: ---------------- <html><head></head><body><script type="text/javascript"> var r = /^https?:\/\//g;...
6
by: runsun pan | last post by:
Hi I am wondering why I couldn't get what I want in the following 3 cases of re: (A) var p=/(+-?+):(+)/g p.exec("style='font-size:12'") -- // expected
27
by: SQL Learner | last post by:
Hi all, I have an Access db with two large tables - 3,100,000 (tblA) and 7,000 (tblB) records. I created a select query using Inner Join by partial matching two fields (X from tblA and Y from...
2
by: =?Utf-8?B?QmlsbEF0V29yaw==?= | last post by:
Hi, I'm using RegExp to try and match a particular string value and can't figure out why it's not working! Possible valid values for the string are EITHER a single number (any length of digits)...
1
by: labmonkey111 | last post by:
I've been using regular expressions for a few months, but I've hit a problem I can't figure out. I need a MySQL regexp to match a string made up of a sequence of characters occurring only 0 or 1...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.