简体   繁体   中英

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

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.

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" . The captured groups can be accessed via reference by $<group> .

You can test your Regular Expressions here: http://regexr.com/

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