Java regex to match only words in double square brackets


I'm trying to do this: I have this kind of text (i.e. a file):

[[dadasd sadasd sdsd ad asddd]] [[dasdsd]] dsdsd [[dsdas]] ... [[dd ssas dd]]

I want only the sentences between double square brackets. How can I solve this with java?

//This one is not working: String patternStr = "(.*)\\[\\[(.*)\\]\\](.*)"; Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(""); // Set the input matcher.reset("[[sdasd]] ddd [[ddssssssssssss]] vvvddd [[dd]] asdasda [[asdsa]] "); ...

Thanks in advance


You want the following regex:


which then translates to a Java string as:


PowerShell test:

PS Home:\> [regex]::Matches("[[sdasd]] ddd [[ddssssssssssss]] vvvddd [[dd]] asdasda [[asdsa]] ", '\[\[(.+?)\]\]') | ft -auto Groups Success Captures Index Length Value ------ ------- -------- ----- ------ ----- {[[sdasd]], sdasd} True {[[sdasd]]} 0 9 [[sdasd]] {[[ddssssssssssss]], ddssssssssssss} True {[[ddssssssssssss]]} 14 18 [[ddssssssssssss]] {[[dd]], dd} True {[[dd]]} 40 6 [[dd]] {[[asdsa]], asdsa} True {[[asdsa]]} 55 9 [[asdsa]]

You regex simply modified: \[\[(.*?)\]\] or this one: \[\[([^\[\]]*)\]\]

So result sentences are in group No 1


Or use this regex: (?<=\[\[)[^\[\]]*(?=\]\]) with lookahead and lookbehind.



You can do something like this:

Pattern pattern = Pattern.compile("\\[+(.*?)\\]+"); Matcher matcher = pattern.matcher("[[sdasd]] ddd [[ddssssssssssss]] vvvddd [[dd]] asdasda [[asdsa]] "); while(matcher.find()) { System.out.println(matcher.group(1)); }

It will output:

sdasd ddssssssssssss dd asdsa



  • How to check if folder is empty with Python?
  • jq: translate array of objects to object
  • in Jade, why can i sometimes use variables as-is and on other times have to enclose them in #{…}?
  • grails add validation error to hasErrors
  • AWK
  • Why does pd.concat change the resulting datatype from int to float?
  • How to prevent a UIView from consuming user input
  • Write cookies from subdomain and read from another subdomain without changing web.config
  • Bounding csv data to topojson path - cloropleth
  • Sending PNG attachment via Android GMail app
  • get value from xml with r by attribute
  • Advertising Identifier for devices lower than iOS 6.0
  • Encode string to Base64 in Inno Setup (Unicode Version of Inno Setup)
  • Velocity (VM) template request parameters: Getting GET variables
  • Arc gradients in Flutter?
  • Homomorphic filtering in OpenCV
  • ASP.NET GridView throws: The version of SQL Server in use does not support datatype 'date'
  • Facebook friend list in Facebook Android SDK 3.14
  • Django REST framework - HyperlinkedRelatedField with additional parameter
  • Getting Microsoft Calibri font on Amazon EC2 ubuntu
  • PHP: Need to close STDIN in order to read STDOUT?
  • Why do you need 2 Javascript files for cross-platform Cordova plugin?
  • Debugging php script timeout?
  • Spotify cocoalibspotify offline status set to 1 but all tracks stuck at waiting
  • Pandas vs matplotlib datetime
  • Creating 2d platforms using JavaScript
  • How to define something in JavaScript [closed]
  • PHPMailer return to AJAX
  • Google Spreadsheet Script to Blink a range of Cells
  • Neo4j…how to get a visual representation of my data?
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • Another “Cannot make static reference…” Question
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • Unity3d lost directional light shadows after generate assetBundle (.unity3d file)
  • Apple Mach-O Linker error (“duplicate symbol”)
  • Bitrate JWplayer
  • Call Microservice from another Microservice within Docker
  • Accessing Arguments, Workflow Variables from custom activities
  • How to mutate multiple variables without repeating codes?