Regular Expression in Java

Posted on 2012-08-14
Last Modified: 2012-08-14

Can someone explain this pattern to me in detail?



Question by:DarrenD
    LVL 18

    Author Comment

    The actual pattern is : "(?:\\*[FMC]\\*)?([\\d\\.]+)(A)?"
    LVL 74

    Accepted Solution

    (?:    -  Start of non-capturing group
    \*     -  Literal star
    [FMC]  -  Either an F, M, or C
    \*     -  Literal star
    )      -  End of non-capturing group
    ?      -  Make the entire group optional
    (      -  Start of capture group 1
    [\d\.] -  Either a digit ( \d ) or a period ( \.)
    +      -  One or more of the the thing to the left (i.e. one or more digits or periods)
    )      -  End of capture group 1
    (      -  Start of capture group 2
    A      -  Literal A
    )      -  End of capture group 2
    ?      -  Make capture group 2 optional

    Open in new window

    The double backslashes are just to satisfy Java. Within string literals, Java can interpret special characters like newlines ( \n ) or tabs ( \t ). However, backslashes are special characters in regex as well. So you have to escape the backslash you want to use in the pattern to satisfy the compiler when it is looking at the string literal.
    LVL 10

    Expert Comment

    Nicely documented, kaufmed.

    Darren - Are you posting because this pattern isn't working as intended?  It seems somewhat odd to define a highly-specific non-capture group, only to make it optional and begin capturing a far more general string of digits and periods.  Obviously, depends on the data you're working with and what you're trying to accomplish.  If this expression isn't working, you might consider posting some sample data and the desired result.
    LVL 18

    Author Closing Comment


    It's an expression written by someone previously and I've not had a lot of experience with regular expressions, at least not with any regularity. (What you don't use, you loose)

    I have it working correctly now as I was able to update the last charachter to [AP] as it can be one of either of these. Apart from that I needed it explained fully as I've never heard of a non-capturing group and couldn't find anything on ?:

    I'll have a look into why the first group in a non-capturing group when I get a chance.



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
    Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    761 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

    10 Experts available now in Live!

    Get 1:1 Help Now