简体   繁体   中英

c# Regex.Replace not working but Regex.Match().value gives correct match

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,

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM