简体   繁体   English

使用REGEX c#将多个数字替换为字符串中的其他数字

[英]Replace multiple numbers with other numbers in a string using REGEX c#

so I have hundreds of these lines below( i trimmed alot), I want to capture the DEFAULT value and set to 0.99 but only for the lines WITH <Movement display_name="Movement type can be session or system ,independent of type and min and max, I am new to REGEX, this is the best I could do, however its not what I want, I know there exist replace in regex but I wasnt able to finish the first part, please help 所以我下面有数百行(我修剪了很多),我想捕获DEFAULT值并将其设置为0.99,但仅对于具有<Movement display_name="Movement type的行可以是会话或系统,独立于类型,min和最多,我是REGEX的新手,这是我能做的最好的事情,但这不是我想要的,我知道regex中存在替换,但是我无法完成第一部分,请帮助

true true false true false true false true false true false true false true false RED BLUE YELLOW GREEN WHITE LIGHT GREY DARK GREY PURE RED ORANGE LIGHT BLUE DARK BLUE MAGENTA LIGHT GREEN 真真假真假真假真假真真假真假真假真假真假真假真假真假真假真假真假真假真假真假真假真假真假真假真假真假真假真假真假真假真假真假真假真假

    <!--Default Values-->
    <Indexed display_name="Indexed" type="system" datatype="pos_int" max="4" min="1" security_level="SU"
     default_value="2" />
    <IndexingColor1 display_name="IndexingColor1" type="system" datatype="list" security_level="SU"
     default_value="RED">

<Cube>
    <ShowFrame display_name="ShowFrame" type="system" datatype="list" security_level="CU" 
     default_value="true">
        <list>
            <ListItem>true</ListItem>
            <ListItem>false</ListItem>
        </list>
    </ShowFrame>
    <ShowFixedPoint display_name="ShowFixedPoint" type="system" datatype="list" security_level="CU" 
     default_value="true">
        <list>
            <ListItem>true</ListItem>
            <ListItem>false</ListItem>
        </list>
    </ShowFixedPoint>
    <FixedPointPosition display_name="FixedPointPosition" type="system" datatype="list" security_level="AU" 
     default_value="0">
        <list>
            <ListItem>-1</ListItem>
            <ListItem>-0.5</ListItem>
            <ListItem>0</ListItem>
            <ListItem>0.5</ListItem>
            <ListItem>1</ListItem>
        </list>
<Timing>
            <FirstPresentation display_name="FirstPresentation" type="system" datatype="float" max="0.0" min="0.0" security_level="CU" 
             default_value="0.0" />
            <Indexing display_name="Indexing" type="system" datatype="float" max="0.0" min="0.0" security_level="CU" 
             default_value="0.0" />
            **<Movement display_name="Movement" type="system" datatype="float" max="0.0" min="0.0" security_level="CU" 
             default_value="0.0" />**
            <Pause display_name="Pause" type="system" datatype="float" max="0.0" min="0.0" security_level="CU" 
             default_value="0.0" />
            <Feedback display_name="Feedback" type="system" datatype="float" max="0.0" min="0.0" security_level="CU" 
             default_value="0.0" />
            <Answer display_name="Answer" type="system" datatype="float" max="0.0" min="0.0" security_level="CU" 
             default_value="0.0" />
            <AutoValidate display_name="AutoValidate" type="system" datatype="float" max="0.0" min="0.0" security_level="CU" 
             default_value="0.0" />
        </Timing>

MatchCollection Collections = Regex.Matches(parameters, "<Movement display_name=\"Movement\" type=\"(<type> .*?).*? min=\"(<min> .*?) .*? max=\"(<max> .*?)\" />", RegexOptions.Singleline);

foreach(Match match in Collections)
{
     Console.WriteLine(match.Groups["type"].Value);
     Console.WriteLine(match.Groups["max"].Value);
}

You can use a regex like this: 您可以使用以下正则表达式:

max=".*?" 

And use the replacement string: 并使用替换字符串:

max="0.99"

Working demo 工作演示

string input = "YOUR STRING HERE";

Regex rgx = new Regex("max=\".*?\"");
string result = rgx.Replace(input, "max=\"0.99\"");

Console.WriteLine("Original String: {0}", input);
Console.WriteLine("Replacement String: {0}", result);

As you mentioned you need to capture and replace the max value, I will take into account only that non- capturing group . 正如您提到的,您需要捕获并替换最大值 ,我将仅考虑该非捕获组

The special character \\s+ is used to indicate one or more white spaces. 特殊字符\\s+用于表示一个或多个空格。

Regex reg = new Regex("(Movement\s+display_name=\"Movement\"\s+type=\".*\" .*min=\".*\"\s+max=\").*(\")");

Now you can replace the not captured group , I mean the "max" value: 现在,您可以替换未捕获的组 ,我的意思是“最大”值:

reg.Replace(textToSearch, "$10.99$2");

What I did here was keep the captured groups and only replace the not captured value: "$1<replacement>$2" . 我在这里所做的是保留捕获的组 ,仅替换未捕获的值: "$1<replacement>$2" The captured groups can be accessed via reference by $<group> . 捕获的组可以通过$<group>引用来访问。

You can test your Regular Expressions here: http://regexr.com/ 您可以在此处测试您的正则表达式http : //regexr.com/

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

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