RegExp: How to match on exact string only?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Findlay

    #1

    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



  • Lee

    #2
    Re: RegExp: How to match on exact string only?

    Mark Findlay said:[color=blue]
    >
    >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.ex e". But I only want bFound to be TRUE is if the entire word
    >matches.
    >
    >Can anyone assist with the correct syntax?[/color]

    The correct syntax is:

    if(Word1 == Word2)
    {
    bFound = true;
    }


    It's a waste of processor time to use regexp's for an exact match.

    Comment

    • Mark Findlay

      #3
      Re: RegExp: How to match on exact string only?

      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" <REM0VElbspamtr ap@cox.net> wrote in message
      news:c81j9j02jb o@drn.newsguy.c om...[color=blue]
      > Mark Findlay said:[color=green]
      > >
      > >I am trying to figure out how to set up my reg exp search so that the[/color][/color]
      search[color=blue][color=green]
      > >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.ex e". But I only want bFound to be TRUE is if the entire word
      > >matches.
      > >
      > >Can anyone assist with the correct syntax?[/color]
      >
      > The correct syntax is:
      >
      > if(Word1 == Word2)
      > {
      > bFound = true;
      > }
      >
      >
      > It's a waste of processor time to use regexp's for an exact match.
      >[/color]


      Comment

      • Lee

        #4
        Re: RegExp: How to match on exact string only?

        Mark Findlay said:
        [color=blue][color=green][color=darkred]
        >> >Here is the current problem code:
        >> >
        >> >Word1 = "RealPlayer.exe "
        >> >Word2 = "Player.exe "
        >> >
        >> >RegExp re = Word2;
        >> >if (re.Find(Word1) )
        >> >{
        >> > bFound = TRUE;
        >> >}[/color][/color][/color]
        [color=blue]
        >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?[/color]


        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("^"+Word 2+"$");
        bFound = re.test(Word1);

        Comment

        • Grant Wagner

          #5
          Re: RegExp: How to match on exact string only?

          Mark Findlay wrote:
          [color=blue]
          > 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[/color]

          Word1 = "^Player.ex e"; // "^" denotes it must match from the start of line
          // or
          Word1 = "^Player.ex e$";
          // "^" 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:

          />
          <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 <gwagner@agrico reunited.com>

          * Client-side Javascript and Netscape 4 DOM Reference available at:
          *


          * 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


          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: RegExp: How to match on exact string only?

            Grant Wagner <gwagner@agrico reunited.com> writes:
            [color=blue][color=green]
            >> Word1 = "RealPlayer.exe "
            >> Word2 = "Player.exe "[/color][/color]
            [color=blue]
            > of course, as has been already pointed out, a regex match against
            > "^...$" is the same as testing if the two strings are equal.[/color]

            .... 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 - lrn@hotpop.com
            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • Steve van Dongen

              #7
              Re: RegExp: How to match on exact string only?

              "Mark Findlay" <mfindlay@speak easy.org> wrote:
              [color=blue]
              >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.ex e". But I only want bFound to be TRUE is if the entire word
              >matches.
              >
              >Can anyone assist with the correct syntax?[/color]

              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

              Comment

              Working...