Rahul writes:
....
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.