c#regex - <pre>

时间:2015-11-12 10:37:01

标签: c# regex

Could you help me sort some regex.

I want all the whitespace before the <pre>

public Regex BeforePre = new Regex(@"^.*?(?=<pre>)", RegexOptions.Compiled);

That will select everything before the <pre>, not just the whitespace. I want to only select the whitespace.

using http://regexstorm.net/tester

Pattern: ^.*?(?=<pre>)

Input: dewjdoiejw; test <pre>frfef</pre>

I believe it's \s but I can not get it to work.

Also I would like it so it works across multiple lines, for example:

Input: dewjdoiejw; test 

<pre>frfef</pre>

Please let me know if you need more info.

UPDATE

see.... http://goo.gl/8mX5c3

I want to remove all white space and tabs before <pre>

I only want to consider all text before <pre> and from that text remove all white-space/tabs.

UPDATE

from

<div>
    <div>
       some text

        <div>some text</div>     
    </div>

    <div>some text</div>   

</div>

<pre>
   <div>
      <div>some text

      <div>some text</div>
    </div>

   <div>some text</div>

   </div>
</pre>

to

<div><div>some text<div>some text</div></div><div>some text</div></div>
<pre>
   <div>
      <div>some text

      <div>some text</div>
    </div>

   <div>some text</div>

   </div>
</pre>

1 个答案:

答案 0 :(得分:1)

好的:只使用一个正则表达式是可能的:

(?s)([\s\r\n]*(?<value></?.*?>)[\s\r\n]*|(?<=</?.*?>)[\s\r\n]*(?<value>.*?)[\s\r\n]*(?=</?.*?>))(?=.*\n<pre>)

很短 - 我知道: - )

Code demo

抱歉 - 但请不要向我解释。