Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-08-2008, 06:20 AM
|
#1
|
|
Member
Registered: Sep 2005
Location: kannur
Posts: 59
Rep:
|
regular expression in java
hi,
how can i use regular expressions in java
i have string contain the following details
for example,
applicationName:id1/id2/id3#description
how can i write a regular expression for finding the pattern applicationName:id1/id2/id3# in the input string.
i wrote like this ^[A-Z]:[0-9]/[0-9]/[0-9]#
is it correct or not
Please help me
|
|
|
|
12-08-2008, 06:55 AM
|
#2
|
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,375
Rep: 
|
you appear to be missing the quantifiers, how many of each pattern
^[A-Z]*:
Should match any number of uppercase characters (assuming it is case sensitive) followed by a colon
^[A-Za-z]*:
Should match any number of characters followed by a colon
Assuming that id1 a string of numbers, you would then want to add [0-9]*
|
|
|
|
12-08-2008, 08:41 AM
|
#3
|
|
Senior Member
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 4,554
|
In my experience, it definitely pays to use several regular expressions when dealing with a complicated structure.
For instance, given the input-data applicationName:id1/id2/id3#, let the first expression look for something that looks like "the whole thing" and that extracts from it applicationName and id1/id2/id3, as two strings. Then, use another regular-expression to break down the second string. If you find that either of the two parts "don't look right," run the first regular-expression again looking for the next available match... it was a "false positive."
Regular-expressions are very powerful, but if you try too hard to get "fancy" with them, they become quite incomprehensible and therefore un-maintainable... which defeats the essential purpose. When even the "puniest" laptop can execute tens or hundreds of millions of instructions per-second, and throws-away 99.5% of that resource anyway, you don't have to be excessively "efficient." Just be clear. Don't make the next programmer to follow your path (even if that programmer is "you") stop-and-think about what you did.
So, I'd say that the first pattern would be: "look for a whitespace-or-start of line, followed by one or more alphanumerics (catch this as group #1), followed by a colon, followed by a group of one or more (say...) '0-9 plus forward slash' (catch this as group #2), followed by a hash-sign." That's a reasonably-specific pattern that will probably obtain good matches, and it won't be complicated for the regular-expression to parse. Then, if you merely "split" the second string on forward-slash, you have merely to verify that none of the pieces are empty.
Do write code that explicitly tests your assumptions ... that each of those split pieces conform to what you know an "id" to be. Your programs need to respond meaningfully and informatively to any input they receive, good or bad. (Here, you are saving hours of very-expensive human time.)
Last edited by sundialsvcs; 12-08-2008 at 08:48 AM.
|
|
|
|
12-09-2008, 07:34 PM
|
#4
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,966
Rep: 
|
I would also take advantage of the fact that you can add strings and literals arbitrarily in Java. You can use that to define components, then add them to form larger expressions. I wrote a configuration parser a few years ago in bash using this sort of pattern assembly.
Code:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Test
{
public static void main(String[] args)
{
String whitespace = "[ \t]";
String maybe_whitespace = whitespace + "*";
String always_whitespace = whitespace + "+";
String token_character = "[A-Za-z0-9_]";
String token = token_character + "+";
String optional_token = "(|" + token + ")";
String assignment = "=";
String single_assignment = assignment + maybe_whitespace + token;
String empty_assignment = assignment + maybe_whitespace + optional_token;
String meta_assignment = "(" + single_assignment + ")+(|" + empty_assignment + ")";
String any_assignment = "(" + empty_assignment + "|" + meta_assignment + ")";
String statement_separator = "(^|$|[;\n])";
String statement_end = "(" + always_whitespace + "|" +
statement_separator + ")";
String assignment_statement = statement_separator + maybe_whitespace +
token + maybe_whitespace + any_assignment +
statement_end;
Pattern assignment_test = Pattern.compile(assignment_statement);
Matcher assignment_find =
assignment_test.matcher("\tone = 4;blank= ; c=d=e=3; ^=1; s=t=; a==b; a=b");
System.out.println("expression: '" + assignment_statement + "'");
int previous = 0;
while (assignment_find.find(previous))
{
System.out.println("found: '" + assignment_find.group() + "'");
previous = assignment_find.end() - 1;
}
}
};
ta0kira
PS Sorry, I keep adding to my example. It's getting out of hand...
Last edited by ta0kira; 12-09-2008 at 07:55 PM.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:05 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|