Skwiggs
Skwiggs

Reputation: 1436

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:

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

Upvotes: 1

Views: 1821

Answers (3)

Jirka Picek
Jirka Picek

Reputation: 599

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.

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

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

Upvotes: 1

dlamblin
dlamblin

Reputation: 45321

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.

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));
 }
 m.appendTail(sb);
 System.out.println(sb.toString());

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

Upvotes: 0

Phu Ngo
Phu Ngo

Reputation: 921

Pure RegEx pattern: \((?=\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

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

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.

Upvotes: 2

Related Questions