Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Regular Expression in Java

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

Can someone explain this pattern to me in detail?



Question by:DarrenD
  • 2
LVL 18

Author Comment

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

Accepted Solution

käµfm³d   👽 earned 2000 total points
ID: 38291562
(?:    -  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

ID: 38291728
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

ID: 38291797

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.



Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…
Suggested Courses

577 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