I need to parse the following string format:
property1 value1
property2 value2
property3 val.ue3
where the left word is the property and the following word is its value. The value should be trimmed at (\n
, \r
, ).
I am using the following code:
Regex reg = new Regex(string.Format("{0}\\s\\w+", propertyName));
string Val = reg.Match(str).Value;
but it has a few issues, and I am having a hard time trying to solve them:
.
, it trims the string there (e.g. for property3
it returns val
but it should return val.ue3
)value2\r
)I would use this
Regex reg = new Regex(string.Format(@"{0}\s+[^\r\n]+", propertyName));
So,if you have a list of propertyNames
var output=propertyNames.Select(x=>
new{
PropertyName=x,
Value=Regex.Match(input,string.Format(@"(?<={0}\s+)[^\r\n]+",x))
.Value
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments