Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Regx.Replace(string, string) not working

string regx replace

Best Answer hanzibal, 12 February 2013 - 11:29 AM

The RegEx variant of Replace is quite different from the ordinary String.Replace method. Regular expression strings can contain certain letters and patterns (like for instance paranthesis) that have special meaning. These are reserved and so if the string you pass in contains any of these you would typically get unexpected results.

 

An example where s+ means one or more white space characters:

var rx = new RegEx("s+");Debug.Print(rx.Replace("sushi is good", "-")); 

The above would give yield the following resulting string:

 

sushi-is-good

 

But if you specify a non reserved pattern, like a literal "s", the method should work the same way as the ordinary Replace method would:

var rx = new RegEx("s");Debug.Print(rx.Replace("sushi is good", "-")); 

-u-hi i- good

 

Does this answer your question?

Go to the full post


  • Please log in to reply
2 replies to this topic

#1 Duefectu

Duefectu

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 12 February 2013 - 08:56 AM

Hello:

 

I want to make a simple string.Replace(string, string), I see in documentation that is implemented in System.Text.RegularExpression in the RegEx class, but can't find the way.

 

Thanks for your time!



#2 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 12 February 2013 - 11:29 AM   Best Answer

The RegEx variant of Replace is quite different from the ordinary String.Replace method. Regular expression strings can contain certain letters and patterns (like for instance paranthesis) that have special meaning. These are reserved and so if the string you pass in contains any of these you would typically get unexpected results.

 

An example where s+ means one or more white space characters:

var rx = new RegEx("s+");Debug.Print(rx.Replace("sushi is good", "-")); 

The above would give yield the following resulting string:

 

sushi-is-good

 

But if you specify a non reserved pattern, like a literal "s", the method should work the same way as the ordinary Replace method would:

var rx = new RegEx("s");Debug.Print(rx.Replace("sushi is good", "-")); 

-u-hi i- good

 

Does this answer your question?



#3 Duefectu

Duefectu

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 13 February 2013 - 08:21 AM

Thanks for your response hanzibal, that's exactly what I needed!

:D

 

Regards!







Also tagged with one or more of these keywords: string, regx, replace

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.