简体   繁体   English

C#Regex.Replace无法正常工作,但Regex.Match()。value给出正确的匹配

[英]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. 我正在尝试进行正则表达式替换,但它似乎不起作用,但是当使用相同的正则表达式检查Regex.Match()。Value的匹配项时,我发现它与我想要的匹配。 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> resultList是一个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. 目前,我正在尝试删除说明,图标和glasswareId。 This is a string I get back from a web server. 这是我从Web服务器获取的字符串。

[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. 名称:Blue Moon Vintage Blonde, 描述:我们永远都不知道接下来会激发我们的酿酒大师Keith Villa。 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. 对于我们限量版的Vintage Blonde,它恰好是来自加利福尼亚的葡萄和27年的下班后业余爱好。Keith对酿造啤酒有着明显的热情。 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. 1995年,当他碾碎葡萄并回想他在加利福尼亚和法国葡萄酒之乡的旅行时,脑海中冒出了将啤酒与葡萄酒混合的想法。 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. 清澈透明的口感令他感到惊喜。rnn2006年,随着葡萄酒和精酿啤酒在饮酒者中越来越受欢迎,他认为现在是将其带回来的好时机。 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. 然后在2010年,它恰好获得了美国大啤酒节的金牌。 Knowing an ale this good should be shared, we distributed it in 2011 as Vintage Blonde Aleu2122. 知道应该分享这种淡啤酒,我们在2011年将其分发为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, 所以现在,六年后,Vintage Blonde Ale在全国范围内发行,供所有人欣赏。, 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. 每次在foreach中更改s ,您只是在更改局部变量s指向的内容,而不是列表本身中的字符串。
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). 另外,对Trim()的调用根本无效,因为您需要将新创建的字符串分配给某些内容( Trim()不会修改原始字符串,实际上,字符串是不可变的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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