正則表達式 - 匹配括號而不匹配其內容

[英]Regex - Match parentheses without matching their contents


I just need to match parentheses around some content that has to match specific criteria. I need to match only the parentheses so that I can then do a quick replacement of only those parentheses and keep their content.

我只需要圍繞一些必須符合特定條件的內容匹配括號。我只需要匹配括號,這樣我就可以快速替換那些括號並​​保留其內容。

For the moment, what I have matches those specific parentheses, but unfortunately also their contents: \((?:\d{2,7})\)

目前,我所擁有的與那些特定的括號相匹配,但遺憾的是它們的內容:\((?:\ d {2,7})\)

The criteria for matching parentheses are as following:

匹配括號的標准如下:

  • only match parentheses that contain \d{2,7}
  • 只匹配包含\ d {2,7}的括號

I have tried positive lookahead (\((?=\d{2,7})\)), and while it does indeed not consume whatever follows the open parenthesis, it then fails to match the closing parenthesis as it backtracks to before the content...

我已經嘗試過積極前瞻(\((?= \ d {2,7})\)),雖然它確實沒有消耗開括號后面的任何內容,但它無法匹配右括號,因為它回溯到之前內容...

So yeah, any help would be appreciated :)

所以,是的,任何幫助將不勝感激:)

3 个解决方案

#1


1  

Pure RegEx pattern: \((?=\d{2,7}\))|(?<=\()\d{2,7}\K\)

純RegEx模式:\((?= \ d {2,7} \))|(?<= \()\ d {2,7} \ K \)


Update: I don't know about Swift, but according to this documentation, Template Matching Format part, $n can also be used similarly, as in

更新:我不知道Swift,但是根據這個文檔,模板匹配格式部分,$ n也可以類似地使用,如

let myString = "(32) 123-323-2323"
let regex = try! NSRegularExpression(pattern: "\\((\\d{2,7})\\)")
let range = NSMakeRange(0, myString.characters.count)
regex.stringByReplacingMatchesInString(myString,
                                       options: [],
                                       range: range,
                                       withTemplate: "$1")

With the assumption that you are using Java, I would suggest something as simple as str.replaceAll("\\((\\d{2,7})\\)", "$1")

假設您正在使用Java,我建議像str.replaceAll(“\\((\\ d {2,7})\\)”,“$ 1”這樣簡單的東西。

The pattern \((\d{2,7})\) captures the whole expression with parantheses with the number in group 1 and replaces it with only the number inside, thus effectively removing the surrounding brackets.

模式\((\ d {2,7})\)使用帶有組1中數字的parantheses捕獲整個表達式,並僅用內部數字替換它,從而有效地刪除周圍的括號。

#2


1  

The regex can be \((\d{2,7})\). It will match all pairing parenthesis with content and the content is accessible via parameter 1 and can be added to string which replace the parenthesis.

正則表達式可以是\((\ d {2,7})\)。它將匹配所有配對括號與內容,內容可通過參數1訪問,並可添加到替換括號的字符串。

How to access results of regex is language specific, I think.

我認為如何訪問正則表達式的結果是特定於語言的。

EDIT:

Here is code which can work. It's untested and I have to warn you at first:

這是可以工作的代碼。這是未經測試的,我必須先警告你:

This is my first experience with Swift and online sandbox which I found couldn't compile it. But it couldn't compile examples from Apple website, either...

這是我第一次使用Swift和在線沙盒,我發現無法編譯它。但它無法從Apple網站編譯示例,或者......

import Foundation

let text = "some input 22 with (65498) numbers (8643)) and 63546 (parenthesis)"
let regex = try NSRegularExpression(pattern: "\\((\\d{2,7})\\)", options: [])

let replacedStr = regex.stringByReplacingMatchesInString(text, 
                options: [], 
                range: NSRange(location: 0, length: text.characters.count), 
                withTemplate: "$1")

#3


0  

Are you okay with removing all parenthesis regardless?: [()] done.
Apparently you've said that's not okay, though that wasn't clear at the time of the question first being asked.

你是否可以刪除所有括號,無論如何?:[()]完成。顯然你已經說過不行,雖然在問到第一個問題的時候還不清楚。

So, then try capturing the number part and using it as the substitution of the match. Like: in.replaceAll("\\((\\d{2,7})\\)","$1"). To put this very plainly so that any regular expression system can use it:
Match:\(([0-9]{2,7})\) means a ( a subgroup of 2 to 7 digits and a )
Substitute each match with: a reference to that match's first subgroup capture. The digits.

所以,然后嘗試捕獲數字部分並將其用作匹配的替代。例如:in.replaceAll(“\\((\\ d {2,7})\\)”,“$ 1”)。為了使任何正則表達式系統都可以使用它:匹配:\(([0-9] {2,7})\)表示一個(2到7位的子組和a)用每個匹配替換每個匹配:對該匹配的第一個子組捕獲的引用。數字。

You can see this operating as you asked on the input:

您可以在輸入中詢問此操作:

Given some (unspecified) input that contains (1) (12) (123) etc to (1234567) and (12345678) we munge it to strip (some) parentheses.

給定一些(未指定的)包含(1)(12)(123)等到(1234567)和(12345678)的輸入,我們將它用於剝離(一些)括號。

If you follow this fiddle.re link, and press the green button. Or with more automatic-explanation at this regex101.com link.

如果您按照此fiddle.re鏈接,然后按綠色按鈕。或者在此regex101.com鏈接中使用更多自動說明。

Or what about replacing each match with a substring of the matched content, so you don't even need a subgroup, since you never want the first or last character of the match. Like:

或者用匹配內容的子字符串替換每個匹配怎么樣,所以你甚至不需要子組,因為你永遠不需要匹配的第一個或最后一個字符。喜歡:

 Pattern p = Pattern.compile("\\(\d{2,7}\\)");
 Matcher m = p.matcher(mysteryInputSource);
 StringBuffer sb = new StringBuffer();
 while (m.find()) {
     m.appendReplacement(sb, mysterInputSource.substring(m.start()+1, m.end));
 }
 m.appendTail(sb);
 System.out.println(sb.toString());

Since you're not using Java, try translating any of the above suggestions to your language.

由於您沒有使用Java,請嘗試將上述任何建議翻譯成您的語言。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/10/17/72f02e833d5678218744e83f7bc418f5.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com