1

我需要解析以下字符串格式:

property1 value1
property2 value2
property3 val.ue3

其中左边的词是属性,后面的词是它的值。该值应在 ( \n, \r, ) 处进行修剪。

我正在使用以下代码:

Regex reg = new Regex(string.Format("{0}\\s\\w+", propertyName));
string Val = reg.Match(str).Value;

但它有一些问题,我很难尝试解决它们:

  • 如果值有 a .,它会修剪那里的字符串(例如,property3它返回val但它应该返回val.ue3
  • 它不会修剪新行或空格中的值(有时它会返回value2\r
4

5 回答 5

1

我会用这个

Regex reg = new Regex(string.Format(@"{0}\s+[^\r\n]+", propertyName));

所以,如果你有一个 propertyNames 列表

 var output=propertyNames.Select(x=>
            new{
               PropertyName=x,
               Value=Regex.Match(input,string.Format(@"(?<={0}\s+)[^\r\n]+",x))
                          .Value
             });
于 2013-09-15T15:33:19.220 回答
1

if the value has a "." it trims the string there (i.e for property3 it returns val but it should return value3)

That's because \\w+ matches alphanumeric characters and underscore, it doesn't match dot characters ..

it doesn't trim the value in a new line or a space (sometime it returns - "value2\r")

I can see how this might be happening because as I said above a \\w+ matches word characters so once it spots any other character it stops matching.

A better regex:

Since the name of the property is passed in, we have one task left and that is to match the value, since values are always to end with a newline \n, carriage return \r or dots . then we could match one or more characters that are neither of those to capture the value, something like this:

{0}\\s*([^\\r\\n ]+)
               ^^
          There is a space here, don't forget it

Notice there is a single space after the \\n in the character class above.

RegexHero Demo

于 2013-09-15T15:35:33.750 回答
0

我不认为你需要一个正则表达式。Split切芥末的方法应该是:

string input = 
@"property1 value1 
property2 value2 
property3 val.ue3";
IList<KeyValuePair<string, string>> result =
    (from line in input.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
    let parts = line.Split(' ')
    where parts.Length > 1
    select new KeyValuePair<string, string>(parts[0], parts[1])).ToList();

现在您可以使用包含键值对的结果:

property1: value1
property2: value2
property3: val.ue3
于 2013-09-15T15:34:47.233 回答
0

\w匹配任何字母、数字或下划线(请参阅单词字符以了解精确定义),但不匹配文字.。为此,您可以使用字符类,例如 [\w.].

此外,如果你是从其他字符串构造模式,你真的应该使用Regex.Escape这样的:

Regex reg = new Regex(string.Format(@"{0}\s[\w.]+", Regex.Escape(propertyName)));
string Val = reg.Match(str).Value;

或者可能省略string.Format

Regex reg = new Regex(Regex.Escape(propertyName) + @"\s[\w.]+");
string Val = reg.Match(str).Value;

请注意使用@来创建逐字字符串文字。这通常使正则表达式更易于阅读,因为您不需要\在模式中转义。

于 2013-09-15T15:35:08.700 回答
0

将您的字符串放入字典并使用它似乎更合适。

var dict =
    str.Split(new char[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries)
       .Select(x => x.Split(new char[] {' '}, 2))
       .ToDictionary(x => x[0], x => x[1]);

string val = dict[propertyName];

嘿,它有效!

于 2013-09-15T15:32:54.170 回答