-1

I'm converting a lot of code from legacy to maintainable and I'm creating a list of regex we can use to do all the pages quickly and the same. My regex skills are that of a child running with a knife...its not great. I've looked up a lot of different ways to only find the first set but I can't seem to get it to work. Can anyone solve this specific problem for me?

Here is the regex search and replace I'm using.

regex: (rs.*)\.Fields\[\"(\w+)\"\].Value

replace: $1.GetValue<object>("$2")

Works

code to search: ...rsProducts.Fields["Price"].Value...

result: rsProducts.GetValue<object>("Price")

This, as I want it to, finds the rs (recordset) of something and changes the way that we extract the value to use an extension method.

Does Not Work

code to search: ...rsProducts.Fields["Price"].Value + rsProducts.Fields["Price2"].Value...

result: rsProducts.Fields["Price"].Value + rsProducts.Fields["Price2"].Value

should be: rsProducts.GetValue<object>("Price") + rsProducts.GetValue<object>("Price2")

In this case the search does match 2 distinct instances but instead it matches the entire line. Here's a pic from regexr.com.

// sorry I don't have the reputation to post the image as an image but heres the Link to Example Image

rayepps
  • 2,072
  • 1
  • 12
  • 22
  • Use lazy dot matching pattern - [`(rs.*?)\.Fields\[\"(\w+)\"\].Value`](http://regexstorm.net/tester?p=%28rs.*%3f%29%5c.Fields%5c%5b%5c%22%28%5cw%2b%29%5c%22%5c%5d.Value&i=...rsProducts.Fields%5b%22Price%22%5d.Value..%0d%0a...rsProducts.Fields%5b%22Price%22%5d.Value+%2b+rsProducts.Fields%5b%22Price2%22%5d.Value...&r=%241.GetValue%3cobject%3e%28%22%242%22%29). – Wiktor Stribiżew Feb 10 '17 at 21:41

1 Answers1

1

You're not dealing handling the case for the + between the two.

(rs.*?)\.Fields\[\"(\w+)\"\].Value
Svek
  • 12,350
  • 6
  • 38
  • 69