Solved

regex meaning - java

Posted on 2013-11-19
5
463 Views
Last Modified: 2013-11-20
I have this regex in my Java code. What does it mean? Examples?

"\[(.*?)\]

Regards.
0
Comment
Question by:jazzIIIlove
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 167 total points
ID: 39659956
perl -MYAPE::Regex::Explain -e 'print YAPE::Regex::Explain->new(qr/"\[(.*?)\]/)->explain;'
The regular expression:

(?-imsx:"\[(.*?)\])

matches as follows:
 
NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  "                        '"'
----------------------------------------------------------------------
  \[                       '['
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  \]                       ']'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
0
 
LVL 31

Assisted Solution

by:awking00
awking00 earned 167 total points
ID: 39660145
Basically, anything that starts with [(, ends with )], with any number of (or no) characters other than a newline character in between.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39660472
ozo..
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 166 total points
ID: 39661046
While ozo's post is correct, maybe you are looking for more of an explanation than that. And awking00 is close, but with a slight error and missing one important part, so this is my go at it...

The \[ and \] match literal square brackets [ and ]. The reason for the \ in front of them is to escape them as otherwise they have special meaning.

The ( and ) tell the regex engine to capture whatever matches inside these brackets. You can use whatever text that these capture in your replacement string (if you are using this in a "replace" method call) or you can actually retrieve this text afterwards, if desired.

The .*? in the middle tell the regex engine to match any number of any characters (including no characters) BUT to only match the minimum possible. How do we know this, well the . tells it to match any character, the * tell it to match 0 or more times and the ? makes this expression non-greedy, ie. only take what you need to.

I'm guessing that this last part is possibly what your real question is about, so some examples may help.

Using the above expression, ie.     \[(.*?)\]

on         Hello [World]                       gives you the captured output of         World
on         Hello []                                   gives you an empty string as the captured output
on         [Hello] [World]                    gives you the captured output of          Hello
          (note that you could run the match again to get the "next" match of World)


If you used the (default) greedy expression, ie.       \[(.*)\]                          Note there is no ?

on         Hello [World]                       gives you the captured output of         World
on         Hello []                                   gives you an empty string as the captured output
on         [Hello] [World]                    gives you the captured output of          Hello] [World



If you look at the above, the first two examples for each expression give you the same output, but the third one differs. The non-greedy expression only gave you Hello because that was the MINIMUM needed to make the match succeed. The greedy expression, however, took as much as it could whilst still allowing the match to succeed.

Hope that helps...
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39661833
ah my mistake, ozo's comment looks really messed in my iphone and got confused.
Thanks and this is an equal split.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now