0

I have the text

<column caption='TZ_Shift' datatype='real' name='[total_millis Parameter]' 
param-domain-type='any' role='measure' type='quantitative' 
value='-0.16666666699999999'>

which can also be like

<column caption='TZ_Shift' value='-0.16666666699999999'>

which gets repeated a several times and I want to replace the value part of it .

so I want the result to be something like this

<column caption='TZ_Shift' datatype='real' name='[total_millis Parameter]' 
param-domain-type='any' role='measure' type='quantitative' 
value='-0.73783'>

and

<column caption='TZ_Shift' value='-0.73783'>

respectively where 0.73783 is new val

I referred this question c# regex to find and replace reusing part of the matched text

But I am unable to understand the regex on the solution

can somebody help me out with regex .

Xml parsing should have been the best solution but I am unable to use xml parsing(for some reason).

1
  • Your "want the result to be something like this" looks identical to your original text. Were you wanting to change it to value="-0.73783" like in your second example? Commented Apr 14, 2012 at 12:24

3 Answers 3

2

If you only want to change the contents of every value attribute, then I would simply use
(<column[^>]* value=')([^']+)('[^>]*>) as pattern and replace it with $1-0.73783$3.

In C# this could be done as follows:

Regex re = new Regex("(<column[^>]* value=')([^']+)('[^>]*>)");  
string input = @"<column caption='TZ_Shift' value='-0.16666666699999999'>";
string output = re.Replace(input, "$1-0.73783$3");

(edit: extended regex to only match <column> elements)

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

Comments

0

Try matching against something like (<column((\s+[\-A-Za-z0-9]+=('[^']*')|("[^"]*"))*)\s+value=)(('[^']*')|("[^"]*"))((\s+[\-A-Za-z0-9]+=('[^']*')|("[^"]*"))*\s*>) and replacing with $1'-0.73783'$9

Somebody else can clean that up if they want to, and you can simplify it if you're sure about stuff attribute order and if all you're attribute values are enclosed in just ' instead of " or a possible mix. You may have to complicate it further to specify the column name in the expression.

Comments

0

You don't say whether the input value is always the same or not (i.e., if it's always -0.16666666699999999). If it always is the same, then you don't even need a regex.

s = s.Replace("-0.16666666699999999", "-0.73783");

1 Comment

sorry the input value is different. and always -0.16666666699999999 could exist at other places too so replacing this way is not correct

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.