LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   regular expression in java (https://www.linuxquestions.org/questions/programming-9/regular-expression-in-java-689083/)

sajith 12-08-2008 06:20 AM

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

graemef 12-08-2008 06:55 AM

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]*

sundialsvcs 12-08-2008 08:41 AM

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.)

ta0kira 12-09-2008 07:34 PM

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...

arunmathew1984 12-10-2008 05:40 AM

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


All times are GMT -5. The time now is 11:41 PM.