Solved

regex meaning - java

Posted on 2013-11-19
5
477 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

737 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