0

I am trying to do a regex replacement and it doesn't seem to be working but when checking the match with Regex.Match().Value with the same regex, I see it matches what I want it to match. I am confused as to what it going on here. I am trying to strip out parts of a very long string.

Parallel.ForEach(resultList, s =>
            {
            s = Regex.Replace(s, "description:.*\\.,", string.Empty);
            s = Regex.Replace(s, "icon:.*\\.png,", string.Empty);
            s = Regex.Replace(s, "medium:.*\\.png,", string.Empty);
            s = Regex.Replace(s, "large:.*\\.png,", string.Empty);
            s = Regex.Replace(s, "glasswareId:[ ][0-9],", string.Empty);

            s.Trim();
            }
        );

resultList is a List<string>

and here is a string I am trying to match and do the replacement on

Currently I am trying to remove the description, icon, and glasswareId. This is a string I get back from a web server.

[edit: string edited to contain less] Here is an example string which doesn't work, bolded what I'm trying to remove

name: Blue Moon Vintage Blonde, description: We never know what will inspire our brewmaster, Keith Villa, next. For our limited-edition Vintage Blonde, it just happened to be a grape from California and a 27-year after-work hobby.rnrnKeith has an obvious passion for brewing beer. That goes without saying. But he also has a little-known passion for wine-making. In 1995, while he was crushing grapes and thinking back to his travels through California and French wine country, the idea of blending a beer with a wine popped into his head. Intrigued, he had to give it a shot.rnrnHe started by using the juice of Chardonnay grapes to give the beer a crisp, light quality, very much like a glass of Chardonnay. He then used white wheat as the only grain to create a clean, smooth finish that could not be achieved with other malts. After cellar-aging the batch for a couple of months, he tasted his latest work. He was pleasantly surprised with the clarity and refreshing taste.rnrnIn 2006, with wine and Craft beer growing in popularity with drinkers, he decided it would be a good time to bring it back. After a couple of years at festivals and special events, it started to gain a following. Then in 2010, it just happened to take home a gold medal from the Great American Beer Festivalu00ae. Knowing an ale this good should be shared, we distributed it in 2011 as Vintage Blonde Aleu2122. So now, after six years, Vintage Blonde Ale is released nationally for all to enjoy., abv: 8.5, ibu: 2, glasswareId: 8,

3
  • 1
    For god sake, please please remove every text that is irrelevant for the question! Commented May 26, 2013 at 5:06
  • Can you give us examples where it isn't working? Commented May 26, 2013 at 5:27
  • Added a shortened version of one of the strings to the question. Commented May 26, 2013 at 5:35

1 Answer 1

1

Every time you change s inside the foreach, you are just changing what a local variable s points to, and not the string in the list itself.
You could create a new list (one that is declared outside the loop) and append to it or modify the original list through an index or in another fashion.
Also, the call to Trim() is not effective at all, as you need to assign the newly created string to something (Trim() does not modify the original string, in fact, strings are immutable).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.