Regex - Match parentheses without matching their contents
NickName:Skwiggs Ask DateTime:2016-10-17T20:43:23

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})\)

The criteria for matching parentheses are as following:

  • only match parentheses that contain \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...

So yeah, any help would be appreciated :)

Copyright Notice:Content Author:「Skwiggs」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40086706/regex-match-parentheses-without-matching-their-contents

Answers
Phu Ngo 2016-10-17T12:55:54

Pure RegEx pattern: \\((?=\\d{2,7}\\))|(?<=\\()\\d{2,7}\\K\\)\n\n\n\nUpdate: I don't know about Swift, but according to this documentation, Template Matching Format part, $n can also be used similarly, as in\n\nlet myString = \"(32) 123-323-2323\"\nlet regex = try! NSRegularExpression(pattern: \"\\\\((\\\\d{2,7})\\\\)\")\nlet range = NSMakeRange(0, myString.characters.count)\nregex.stringByReplacingMatchesInString(myString,\n options: [],\n range: range,\n withTemplate: \"$1\")\n\n\n\n\nWith the assumption that you are using Java, I would suggest something as simple as\nstr.replaceAll(\"\\\\((\\\\d{2,7})\\\\)\", \"$1\")\n\nThe 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.",


Jirka Picek 2016-10-17T12:54:49

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.\n\nHow to access results of regex is language specific, I think.\n\nEDIT:\n\nHere is code which can work. It's untested and I have to warn you at first:\n\nThis 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...\n\nimport Foundation\n\nlet text = \"some input 22 with (65498) numbers (8643)) and 63546 (parenthesis)\"\nlet regex = try NSRegularExpression(pattern: \"\\\\((\\\\d{2,7})\\\\)\", options: [])\n\nlet replacedStr = regex.stringByReplacingMatchesInString(text, \n options: [], \n range: NSRange(location: 0, length: text.characters.count), \n withTemplate: \"$1\")\n",


More about “Regex - Match parentheses without matching their contents” related questions

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

Show Detail

Match ONLY inside parentheses in RegEx

I'm taking a beating to build a regular expression. Rules for matching: Must contain the EN string The String must be between parentheses At the starting parentheses, there must be a ! The string...

Show Detail

Python recursive regex for checking matching parentheses?

I'm currently working on a mathematical function parser in python where I'm using regex to match expected forms of functions, e.g. 'f(x,y) = (x^2, x+y)' The current regex pattern for reckognizing s...

Show Detail

More concise/efficient regex to match a string within matching parentheses

I would like to match strings, where foo appears within select([...]), but only if possibly occurring parentheses match together. E.g. match select(((foo))) or select(x(())(foo(x))()x((y)x)x()) but...

Show Detail

Match string with Regex as long as it is not surrounded by parentheses

I am looking to match a string "Order By XXX" where XXX can be any letter, number, period, comma, space or square bracket. However, I would only like to match this if it is not surrounded by parent...

Show Detail

Matching Parentheses with Regex in Flex

For some reason I can't seem to match a parentheses in Flex using Regex to save my life. What's wrong with this? var commandTxt:String = "switchview(adf)"; var switchViewArray:Array = commandTxt....

Show Detail

Matching Parentheses with Division Operator - Regex

Examples: input: (n!/(1+n)) output: frac{n!}{1+n} input: ((n+11)!/(n-k)^(-1)) output: frac{(n+11)!}{(n-k)^(-1)} input: (9/10) output: frac{9}{10} input: ((n+11)!/(n-k)^(-1))+(11

Show Detail

C# Regex match anything inside Parentheses

I want to match anything inside parentheses but the result must exclude the parentheses as well. Examples: Initialize(P90W) Brake(45X) Result: 990W 45X note results without the Parentheses....

Show Detail

Regex pattern matching last word in parentheses containing all parentheses

I need help matching these patterns all at once. I am using Google Sheets REGEXEXTRACT. This is what I have so far: \(.+\) This matches (Jake), (The LGS Regiment) (Bob), and (John (Wake)) from the

Show Detail

Cannot match parentheses in regex group

This is a regular expression, evaluated in .NET I have the following input: ${guid-&gt;newguid()} And I want to produce two matching groups, a character sequence after the ${ and before }, which...

Show Detail