Solved

regex meaning - java

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JDeveloper 12c for 32 bit 4 84
Spring Framework HTTPSession management 1 33
Java string replace 11 43
Problem to Alipay 10 18
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 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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

813 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

11 Experts available now in Live!

Get 1:1 Help Now