Solved

regex meaning - java

Posted on 2013-11-19
5
467 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 32

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

895 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

13 Experts available now in Live!

Get 1:1 Help Now