2013-06-28 45 views
2

我有一個匹配用正則表達式這樣的文字:有沒有辦法在正則表達式中排除單詞或語音?

==================== 
    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 

    Copyright 2008-2009 Company, Inc. All rights reserved. 


    The contents of this file are subject to the terms of the Common Development 
    and Distribution License("CDDL") (the "License"). You may not use this file 
    except in compliance with the License. 

    You can obtain a copy of the License at https://oss.oracle.com/licenses/CDDL 
    See the License for the specific language governing permissions and limitations 
    under the License. 

    When distributing the Covered Code, include this CDDL Header Notice in each file 
    and include the License file at https://oss.oracle.com/licenses/CDDL. 
    If applicable, add the following below this CDDL Header, with the fields 
    enclosed by brackets [] replaced by your own identifying information: 
    "Portions Copyrighted [year] [name of copyright owner]" 
    ==================== 

    Copyright 2011-2013 Company. All rights reserved. 

,並靜從不改變===========之間的部分,這樣我就可以搜索此表達式以靜態的方式表達,但它不是一個格式良好的正則表達式,我怎樣才能使正則表達式中的靜態===========之間的所有文本?

+0

這完全取決於您正在使用的編程語言。哪一個? –

+0

也許是基本的正則表達式格式:'\ = {22} [^ \ =] * \ = {22}'? – 2013-06-28 12:38:31

+2

如果文本是靜態的,請不要使用正則表達式,但嚴格相等。 – Toto

回答

4

如果你不能真正寫代碼,但只有一個正則表達式,你可以用\Q\E關閉所有的元字符的模式的某一部分:

startOfRegex\Q============...\EendOfRegex 

這樣, \Q\E之間的部分可以包含任意的正則表達式元字符(如patrentheses和括號以及星號和反斜槓以及whatnot),而startOfRegexendOfRegex可以是正常和任意的正則表達式模式。

如果字符串包含\E或以反斜槓結尾,則只會出現問題。在那種情況下,Tim Pietzker的Pattern.quote是唯一通用的方法。

+0

非常感謝你,現在這一切都更清楚 – andPat

+1

@andPat:那麼你應該把這個答案標記爲接受,然後。 –

3

Netbeans使用Java,對吧?然後,您可以使用

String regex = Pattern.quote(my_verbatim_string); 

正確地轉義所有正則表達式元字符。

但很有可能,使用像(?s)={20}.*?={20}這樣的正則表達式來查找====================之間的所有文本並進行嚴格的相等比較將會是一個好得多的想法。

+0

所以沒有辦法說:「這兩個標籤」之間的所有文本(無論charachter)是正則表達式的靜態部分,不要解析爲正則表達式,對吧? – andPat

+0

@andPat正是'Pattern.quote'所做的。 –

+0

ahh謝謝我不知道,但我不必使用Java,我只需要寫一個正則表達式,換句話說,我必須用手動引用靜態文本,因爲我必須通過這個正則表達式給一個只輸入純正則表達式的評估器。 – andPat

相關問題