我有4個正則表達式,我得到了起來,他們工作很好,但在一些應用程序,他們釘住了CPU。我知道他們是最好看的正則表達式,但我不確定更好的方法。我能做些什麼來優化這些正則表達式嗎?正則表達式是盯着CPU
public static Regex schemaOrg = new Regex(@"\s*itemtype\s*=\s*('|"")\s*http://schema.org/\s*", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
public static Regex dataVocabulary = new Regex(@"\s*itemtype\s*=\s*('|"")\s*http://data-vocabulary.org/\s*", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
基本上在尋找:
itemtype="http://schema.org/"
itemtype="http://data-vocabulary.org/"
但隨着空格的量,因爲這仍然將是HTML有效。
例如:
itemtype ="http://schema.org/"
itemtype= "http://schema.org/"
itemtype=" http://schema.org/ "
都將是有效的。
更新:仍然包裝了CPU壞。
\s+itemtype\s*=\s*(?:'|"")\s*http://schema\.org/
你是否錯過了結尾的'('|「」)? – dasblinkenlight
@dasblinkenlight:如果有的話,它應該是一個關閉'\ 1'。 –
@TimPietzcker當然它*應該*(並且在答案中*會*),但是在詢問OP是否忘記了尾部引號'('|「」)'的評論比反向引用更容易理解。 – dasblinkenlight