Advertisement
Guest User

balanced brackets

a guest
Jan 28th, 2014
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. Regex:
  2.  
  3. (?x)\[ # First '['
  4. (?:
  5. [^\[\]]+ # Match everything that's not "[]"
  6. |
  7. (?<open> \[ ) # Match '[', and capture into 'open'
  8. |
  9. (?<-open> \] ) # Match ']', and delete the 'open' capture
  10. )+
  11. (?(open)(?!)) # Fails if 'open' stack isn't empty!
  12. \] # Last opening ']'
  13.  
  14.  
  15. Tested with:
  16.  
  17. possible-text [posibility-1/posibility-2] possible-text [[un/]certain/[un/]sure] some-text [I am/I'm] [[un/]certain/[/un]sure]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement