0

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);
}
1
  • <Movement display_name="Movement" type="session" datatype="float" max="8.00" min="4.00" security_level="CU" default_value="8.00" /> Commented Jun 15, 2015 at 1:14

2 Answers 2

1

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/

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

3 Comments

I appreciate your help,the string(xml) is very long as you can see there are alot of default numbers ( default="123") i just want to change the ones that contain bold**<Movement display_name="Movement" type="session" datatype="float" max="8.00" min="4.00" security_level="CU" default_value="8.00" />**bold ---------OR--------- bold**<Movement display_name="Movement" type="system" datatype="float" max="0.0" min="0.0" security_level="CU" default_value="0.0" />**bold I would appreciate if you could write one generic for both and onle only for type="session" @jherax
The answer I provided meets the criteria in your question when you posted first time. Now you have edited the question. You can first mark the correct answer, and later edit your post to add the new criteria. And please, add an EDITED section to be clearer.
thanks, you guys showed me how to do it, I will post the answer shortly
1

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

2 Comments

I appreciate your help, but I didnt explain well(and im new to stackoverflow) the string(xml) is very long as you can see there are alot of default numbers ( default="123") i just want to change the ones that contain <Movement display_name="Movement" type="session" datatype="float" max="8.00" min="4.00" security_level="CU" default_value="8.00" /> ---------OR--------- <Movement display_name="Movement" type="system" datatype="float" max="0.0" min="0.0" security_level="CU" default_value="0.0" /> I would appreciate if you could write one generic for both and onle only for type="session"
thanks, you guys showed me how to do it, I will post the answer shortly

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.