Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Match Meta Characters between strings( Regular Expressions)

Match Meta Characters between strings( Regular Expressions)

Scheduled Pinned Locked Moved C#
regextestingbeta-testinghelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abhinish
    wrote on last edited by
    #1

    Hi! need some help guys i want to match strings as whole words through regular expressions which contain meta characters. i tried the Regex.Escape method which "Guffa" suggested me .. it works good and don't throw any exception but it do not match the whole word also. m i again missing some thing.. here is my code:

    public bool sSearchWholeWord()
    {
    string sSearchWord="***SPAM***";

       string content="hi this is \*\*\*SPAM\*\*\* testing";
    
    string pattern =  @"\\b" + Regex.Escape(sSearchWord) + @"\\b";			       
    
    
       Regex re =new Regex(pattern,RegexOptions.IgnorePatternWhitespace);
    	try			
                {			
                  if(re.IsMatch(content))				
                  {					
                   return true;
                  }
              else				
                 {					
                 return false;				
                  }                        
                }                        
               catch(Exception e)			
                {				
                   Response.Write(e.ToString());				
                   return false;			
                 }                
         }
    

    this returns false but according to me it should return true also if i remove these meta charaters it returns true.. i dont know what m i missing or is there any other way of matching these meta characters? abhinav

    abhinav

    L 1 Reply Last reply
    0
    • A abhinish

      Hi! need some help guys i want to match strings as whole words through regular expressions which contain meta characters. i tried the Regex.Escape method which "Guffa" suggested me .. it works good and don't throw any exception but it do not match the whole word also. m i again missing some thing.. here is my code:

      public bool sSearchWholeWord()
      {
      string sSearchWord="***SPAM***";

         string content="hi this is \*\*\*SPAM\*\*\* testing";
      
      string pattern =  @"\\b" + Regex.Escape(sSearchWord) + @"\\b";			       
      
      
         Regex re =new Regex(pattern,RegexOptions.IgnorePatternWhitespace);
      	try			
                  {			
                    if(re.IsMatch(content))				
                    {					
                     return true;
                    }
                else				
                   {					
                   return false;				
                    }                        
                  }                        
                 catch(Exception e)			
                  {				
                     Response.Write(e.ToString());				
                     return false;			
                   }                
           }
      

      this returns false but according to me it should return true also if i remove these meta charaters it returns true.. i dont know what m i missing or is there any other way of matching these meta characters? abhinav

      abhinav

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      abhinish wrote:

      string pattern = @"\b" + Regex.Escape(sSearchWord) + @"\b";

      Remove the 'word boundry' qualifiers. If you really need to check for a word, wrap specify spaces around the match and do explicit capture.

      **

      xacc.ide-0.2.0.50 - now with partial MSBuild support!

      **

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups