簡體   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