LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 12-08-2008, 06:20 AM   #1
sajith
Member
 
Registered: Sep 2005
Location: kannur
Posts: 59

Rep: Reputation: 15
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
 
Old 12-08-2008, 06:55 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
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]*
 
Old 12-08-2008, 08:41 AM   #3
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,231

Rep: Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071Reputation: 1071
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.
 
Old 12-09-2008, 07:34 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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.
 
Old 12-10-2008, 05:40 AM   #5
arunmathew1984
Member
 
Registered: Nov 2008
Posts: 31

Rep: Reputation: 15
Please lookup examples of the java.util.regex package on google. That should help you get started.

Another resource you might find useful:
http://java.sun.com/docs/books/tutor...sential/regex/

Linux Archive

Last edited by arunmathew1984; 12-20-2008 at 11:16 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expression help chibi2666 Linux - Newbie 3 11-09-2008 10:37 PM
Regular Expression harkonen Programming 6 07-12-2008 12:06 PM
about regular expression '=~m//' littletransformer Programming 7 03-25-2008 08:31 PM
Do we have regular expression in C++ ? indian Programming 4 03-06-2006 09:54 AM
Regular Expression slizadel Programming 4 07-28-2003 05:16 AM


All times are GMT -5. The time now is 11:21 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration