简体   繁体   中英

Get string between with regex

I have string like:

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

And i want to get string between these two tags <plaintext> , </plaintext> My code so far

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);

But my regex get only string in same line but when <plaintext> , </plaintext> tags are on different lines - cant parse it.

You need to add RegexOptions.Singleline to your regex-arguments like this.

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

Then it will capture everything except newlines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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