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 |
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]* |
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.) |
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; PS Sorry, I keep adding to my example. It's getting out of hand... |
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. |