> > I need a regular expression..
> > The conditions are as follows...
[quoted text clipped - 16 lines]
> gordon@panix.com I think that you could be trusted not to shaft
> him." -- Robert Chang, rec.games.board
> Dear John
>
[quoted text clipped - 29 lines]
> > gordon@panix.com I think that you could be trusted not to shaft
> > him." -- Robert Chang, rec.games.board
sounds like hw to me... but nevertheless, something like
^[a-zA-Z/]*$
should work. it will find match anything with only letters and the
slash character. you really could have googled this in about 5 seconds.
i'm pretty sure the idea behind this assignment is to learn how regular
expressions work, not just get the darn thing done.
i love regular expressions :) they're nifty things. very good to know.
very handy.
Jussi Piitulainen - 09 Nov 2006 11:30 GMT
> you really could have googled this in about 5 seconds.
Oh yes, Google. I meant to recommend a way to _find_ the documentation
but I forgot: simply put "java.util.regex.Pattern" to a Google search
box, and a right page is likely to be the very first hit. Put in "1.5"
or something to find a more recent page, maybe, but that does not
matter much in this case.
> i'm pretty sure the idea behind this assignment is to learn how
> regular expressions work, not just get the darn thing done.
Un-doubted-ly.
...
> The text should not contain any other special symbol other than / .
>
> So the condition should be interpreted like, The text would be
> consists of alphabets and / only, not any other characters.
This is still a very simple regexp. You should read Sun's Javadoc for
java.util.regex.Pattern and java.util.regex.Matcher. Really. They are
pretty good, in my opinion, though regexen are a bit complicated, and
you should manage to ignore the more arcane corners. And then you
should experiment some, to get a solid grasp of the basics.
Try this:
import java.util.regex.Pattern; import java.util.regex.Matcher;
class M { public static void main(String [] args) {
Pattern p = Pattern.compile("[a-z/]*"); // <--- your regex
Matcher m = p.matcher(args[0]);
while (m.find()) {
System.out.println("|" + m.group() + "|");
}
}}
Edit the regex, compile, experiment like so:
java -cp . M "vaarallinen/juhlallinen juhannus 2006 tms."
It will print each match it finds in the string, starting from the
left, not overlapping. If it prints nothing, there was no match.
Experiment with different arguments on the command line, but try to
avoid the characters that are problematic in the shell. (Awkward.
That's why I don't suggest providing the regex on the command line:
the same characters tend to be special for both.)
Experiment with ^ in the beginning and $ in the end of the regex, and
with ? and + in place of *, and then with *?, +?, ??, and try to
figure out what "a-z/*" and (a-z/)* match, so as to really understand
that the different kinds of brackets have very different meanings.
(You may find [a-z/]+ and friends a bit easier than the starred
versions. The empty string tends to be a nuisance.)
Try to get some undestanding of the three Matcher methods find,
matches and lookingAt.
Remember that \w, for example, is spelled "\\w" in your source code,
with double backslash, because that is how a backslash is spelled
inside a string.
And just by the way, keep in mind that regexen are not applicable to
everything. For some things, they are an overkill, for others they
lack power. They are fun, though. Have fun.