2017-02-13 10 views
1

正規表現を使用して、文字列の置換を行うのに役立つ必要があります。空の文字列を返す正規表現

タスク:生成されたhtml文字列内のフォントを拡大縮小します。私はQtを使用しています。Qt 4.8で作業する必要があります。

私は、フォントサイズを含むセクションを区切るためにいくつかの正規表現を決定し、それをテストしました(https://regex101.com/r/Y0W13N/1) - 正しいか最適かはわかりませんが、テストサイトは正しい出力を与えてくれます - まだ私は私のコードにはマッチを取得しないように見える:

// get string between "<span style=\"" and "\">" (escaped quotes and backslashes) 
QRegExp rx1("<span style=\"(?:=([^\\]]+))?(.*?);\">"); 
int pos = rx1.indexIn(text); 
QStringList listSpans1 = rx1.capturedTexts(); 
qDebug() << listSpans1;        // outputs ("", "", "") 

// get string between "<p style=\"" and "\">" 
QRegExp rx2("<p style=\"(?:=([^\\]]+))?(.*?);\">"); 
pos = rx2.indexIn(text); 
QStringList listSpans2 = rx2.capturedTexts(); 
qDebug() << listSpans2;        // outputs ("", "", "") 

私がテストしていtext

"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> 
<html><head><meta name="qrichtext" content="1" /><style type="text/css"> 
p, li { white-space: pre-wrap; } 
</style></head><body style=" font-family:'MS Shell Dlg 2'; font-weight:400; font-style:normal;"> 
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Some Font'; font-size:15pt; color:#000000;">Te</span><span style=" font-family:'Some Font'; font-size:9pt; color:#000000;">xt</span></p> 
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Some Font'; font-size:9pt; color:#000000;"></p> 
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Some Font'; font-size:9pt; color:#000000;"> B</span><span style=" font-family:'Some Font'; font-size:15pt; color:#000000;">ox</span></p> 
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Some Font'; font-size:18pt; color:#000000;"></p></body></html>" 

私はqDebugから空の文字列を取得している - テストサイトことを考えると、なぜ私は理解していません私を示すcorr ectの文字列、そして私はマッチを得るように見える?なぜ空白なのですか...

(次のステップは、フォントの部分を区切ることです...フォントサイズを決める...スケールを変更します...置き換える...このような単純な操作では非常に複雑ですが、私は

私が作った正規表現はテストサイトで動作するようですが、私のコードでは動作しません。理由はわかりませんが、明らかに正規表現の経験はありません。

ここでのポイントは、あなたがQtのRegExpで怠惰*?/+?数量詞を使用することができないということであるあなたに

+0

エスケープすべての特殊記号1より多くの時間 - 例えば:第二の正規表現と

QRegExp rx1("<span style=\"(.*);\">"); rx1.setMinimal(true); 

同じ\\\ "、\\ - > \\\\など –

+1

' 'rx1.setMinimal(true)'を使用します。 2番目の正規表現( '

'/'rx2.setMinimal(true)')と同じです。 –

+0

@WiktorStribiżewは答えることができますか? – Thalia

答えて

1

ありがとう...働く私の正規表現を手助けしてください。

あなたは、グループ1つのパターンとして.*パターンを使用してrx1.setMinimal(true)を使用して問題が解決することがあります。

QRegExp rx2("<p style=\"(.*);\">"); 
rx2.setMinimal(true);