Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

.NET Regex.Replace Pattern Question

Last post 01-19-2010, 5:42 PM by Aussie Susan. 1 replies.
Sort Posts: Previous Next
  •  01-19-2010, 11:12 AM 58610

    .NET Regex.Replace Pattern Question

    Hi All,

    Following the guidelines of this forum:

    What programming language/platform or application are you using?

    I am using the .NET framework.  Regex.Replace() is where I'm having a tough time.
     
    What is your ultimate task? What are you working on and what you want to use a Regular Expression for in your planned solution.
     
    I need to be able to take a string that looks like this:
     
    "m.Price + m.PricePrev"
     
    and encapsulate the two operands in brackets so that the string ends up looking like this:
     
    "[m.Price] + [m.PricePrev]" 
     
    what my string currently ends up looking like is this:
     
    "[m.Price] + [m.Price]Prev"  
     
     Describe your Regular Expressions-related problem with the task and what you have attempted so far. Please be fairly descriptive.
     
    My attempt at solving this problem has been to:
     
    1. Use Regex.Matches() to get a match collection containing the identifiers m.Price and m.PricePrev.  this works fine
    2. For each of the matches, I am creating a string which is equal to the match value surrounded by brackets.  i.e. if the match value is "m.Price", I am creating a new string "[m.Price]".
    3. HERE IS MY PROBLEM: using Regex.Replace(), I'm not 100% sure how to specify that "m.Price" should be replaced by "[m.Price]" so that "m.PricePrev" is not affected as well making it "[m.Price]Prev"
     
    I guess what I don't know is, how do you say, "replace any instance of "m.Price" with "[m.Price]" if "m.Price" is not touching any alphanumeric characters on its left or right?"  Any help would be greatly appreciated.
     
    Thanks,
    Paul 
  •  01-19-2010, 5:42 PM 58623 in reply to 58610

    Re: .NET Regex.Replace Pattern Question

    Paul,

    Which parts of your example are fixed and which are variable: could you have "a.Price" or "mmmm.Price" or "x.PriceThatIWasCharged" etc.

    Also, can there be anything else in the text that is passed to the regex that can look something like the target string but is not to be matched: "3.14" for example

    Without seeing the pattern that you tried, I'm guessing that it was something like

    (m\.Price)

    and the replacement string was something like

    [$1]

    If you want to match "m.Price" but NOT "m.PricePrev" or "am.Price" then you might try:

    (\bm\.Price\b)

    where the '\b' means to match a word boundary - in this case the "word boundary" is defined as one of the characters "a-z" and '0-9" or the underscore on one side and any other character on the other (either way around).

    However, if you wan to match some of the other possibilities I suggested above, then you really need to tell us what really makes a match.

    Susan

View as RSS news feed in XML