1

質問:

次のテキスト ファイルがあるとします。

blablabla
# Block 1
# Some text
## Some more text
### Even more text
### Hello
# Some text
### Again Text
# Blank lines or lines not starting with # terminate

blablabala
# Block 1
# Some text
## Some more text
### Even more text
### Hello
# Some text
### Again Text
# Blank lines or lines not starting with # terminate
blablabla

# で始まる行のすべてのブロックを正規表現で抽出することは可能ですか?
注:
ブロックは 1 つの文字列である必要があります。# で始まるすべての行を抽出するだけで簡単です。

追加の質問:
正規表現の先頭の # の数を取得することは可能ですか?

4

2 に答える 2

2

使用する

var regex = new Regex(@"(#.*([\n]|$))+");
var matches = regex.Matches(sample_string);

あなたの例に設定すると、最初のブロックと2番目のブロックのsample_string2つの一致が返されます。matches[0]matches[1]

于 2013-05-05T10:59:31.487 に答える