Hussein Nazzal
Hussein Nazzal

Reputation: 2557

match two open brackets

so let's say i have this

example: 1
[
   this shouldn't be matched
]
example : 2 
[
   some bla
   [
      other bla
   ]

]

i watch to check if the file has any nested brackets . i dont want to actually check if brackets match i only need to see if they exist .

now in my head this sound simple , but i couldnt get nay thing out of this (in terms of regular expressions )

so i came up with something ( a scanner hopefully ) which is just a simple javascrpit function .

function idNested(str){
    // match all brackets
    var g = str.match(/[\[\]]/g);
    // join them into one string
    var b = g.join('');
    // match double bracket if there is any , it means that there is nesting :)
    return b.match(/\[\[/) ? true : false;
}

the first regular expression gets all the brackets . and then i join them into one big string and search for two brackets that follow each other .

so my question is really based of two things .

1 - Is there a regular expression that would just solve my problem ?!

2- Does this function has any downfalls ? if so then please suggest something else .

Upvotes: 1

Views: 515

Answers (2)

Senseful
Senseful

Reputation: 91711

Answering number 2:

No, this shouldn't have any downfalls if your brackets are nested correctly.

Essentially you are reducing a string down to only its brackets. In your case, the string would become [][[]].

Assume we are processing the string 2 characters at a time. There are only 4 possible combinations:

  1. [[ we found a nested bracket, and we are done with matching, since the rest of the string must have closing brackets for these (e.g. [[]]).
  2. [] we found a single set of brackets, ignore this result, and move on.
  3. ][ this can never happen because it implies that we either have already matched 1 (e.g. [[][...) and we are done with algorithm, or you have invalid bracket nesting (e.g. []][).
  4. ]] we would have already found 1 if brackets are nested correctly, and thus would never reach this state.

Thus your algorithm would work.

Answering number 1:

Having said that, the code would be much simpler with a single regular expression (assuming proper nesting):

str.match(/\[[^\]]*\[/)

Strings that would be matched:

  • [[]]
  • [[[]]]
  • [][[]]

Strings that wouldn't be matched:

  • [][]
  • []

Strings we don't care about (since they aren't properly nested):

  • [[
  • ][[
  • [[[]]

Upvotes: 2

Ibrahim Najjar
Ibrahim Najjar

Reputation: 19423

Why not check for this:

\[              # Match an open bracket
[^\]]*?         # Match zero or more non-closing bracket ] lazily
\[              # Match another opening bracket
[^\]]*          # Match zero or more non-closing bracket ] greedily
\]              # Match a closing bracket.

It works like this, if we matched an open brack [, then we look for another opening-bracket [, and we make sure that we don't cross a closing bracket ].

If the above expression matches then there is nesting in the text.

var isNested = '[f daflkd [hfds ] fdaf d[ [] fd'.match(/\[[^\]]*?\[[^\]]*\]/g);

NOTE: This assumes the file doesn't contain impropriety nested brackets.

Regex101 Demo

Upvotes: 2

Related Questions