繁体   English   中英

使用正则表达式获取字符串

[英]Get string between with regex

我有这样的字符串:

 <plaintext>10/2 (2×5 + (10 - 1)×2)</plaintext>
  </subpod>
  <expressiontypes count='1'>
   <expressiontype name='Default' />
  </expressiontypes>
 </pod>
 <pod title='Exact result'
     scanner='Rational'
     id='Result'
     position='200'
     error='false'
     numsubpods='2'
     primary='true'>
  <subpod title=''>
   <plaintext>140</plaintext>
  </subpod>

   <plaintext>Simplify the following:
(10 (2×5 + (10 - 1) 2))/2
(10 (2×5 + (10 - 1) 2))/2 = (10 (2×5 + (10 - 1) 2))/2:
(10 (2×5 + (10 - 1) 2))/2
10/2 = (2×5)/2 = 5:
5 (2×5 + (10 - 1) 2)
10 - 1 = 9:
5 (2×5 + 9×2)
2×5 = 10:
5 (10 + 9×2)
9×2 = 18:
5 (18 + 10)
 | 1 | 8
+ | 1 | 0
 | 2 | 8:
5×28
5×28 = 140:
Answer: | 
 | 140</plaintext>

我想在这两个标签之间获取字符串<plaintext></plaintext>我的代码到目前为止

StringBuilder stringBuilder = new StringBuilder();
Regex regex = new Regex(@"<plaintext>(.*?)</plaintext>");
foreach (Match match in regex.Matches(here is string above)
    stringBuilder.Append(match.Groups[1].Value);

但是我的正则表达式只在同一行中获取字符串,但是当<plaintext></plaintext>标签位于不同的行时 - 无法解析它。

您需要像这样将RegexOptions.Singleline添加到您的正则表达式参数中。

var regex = new Regex(@"<plaintext>(.*?)</plaintext>",
                  RegexOptions.IgnoreCase | RegexOptions.Singleline);

然后它将捕获除换行符之外的所有内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM