Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java regex to match only words in double square brackets

Tags:

java

regex

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

like image 232
Mux Avatar asked Dec 16 '22 11:12

Mux


1 Answers

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]]
like image 137
Joey Avatar answered Dec 30 '22 15:12

Joey