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

[英]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 个解决方案



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)
                                       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捕獲整個表達式,並僅用內部數字替換它,從而有效地刪除周圍的括號。



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.



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...


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")



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.


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


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));

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




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