Regular Expression

R

Rahul

I need a regular expression..
The conditions are as follows...

1. There should not be any blank spaces.
2. Text may or may not contain slashes.

Thanking you
 
J

John Gordon

In said:
I need a regular expression..
The conditions are as follows...
1. There should not be any blank spaces.

^[^ ]*$

(Note that I interpreted "spaces" literally. If you also wanted to exclude
other space-like characters such as tabs, you'll have to modify this
expression.)
2. Text may or may not contain slashes.

I'm not sure why this condition is even listed. It's okay to have slashes,
but it's also okay not to have them. This condition is meaningless.
 
O

Oliver Wong

John Gordon said:
In <[email protected]> "Rahul"
I need a regular expression..
The conditions are as follows...
1. There should not be any blank spaces.

^[^ ]*$

(Note that I interpreted "spaces" literally. If you also wanted to
exclude
other space-like characters such as tabs, you'll have to modify this
expression.)
2. Text may or may not contain slashes.

I'm not sure why this condition is even listed. It's okay to have
slashes,
but it's also okay not to have them. This condition is meaningless.

It depends on whether you think the conditions are inclusive or
exclusive (or additive versus subtractive, or implicit versus explicit,
whichever pair of terms you prefer). I.e. are we allowing all strings, as
long as they don't break any of the above conditions? Or are we not allowing
any strings, unless these conditions allow them? If it's the latter, the RE
might be more like:

^/*$

But yeah, condition 2 is a red flag telling me that the problem is not
well understood, and further clarification would help.

- Oliver
 
R

Rahul

Dear John

Thanks for your response.

The 2nd condition is :

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.

Thanking you.

John said:
In said:
I need a regular expression..
The conditions are as follows...
1. There should not be any blank spaces.

^[^ ]*$

(Note that I interpreted "spaces" literally. If you also wanted to exclude
other space-like characters such as tabs, you'll have to modify this
expression.)
2. Text may or may not contain slashes.

I'm not sure why this condition is even listed. It's okay to have slashes,
but it's also okay not to have them. This condition is meaningless.
 
M

Mark

Rahul said:
Dear John

Thanks for your response.

The 2nd condition is :

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.

Thanking you.

John said:
In said:
I need a regular expression..
The conditions are as follows...
1. There should not be any blank spaces.

^[^ ]*$

(Note that I interpreted "spaces" literally. If you also wanted to exclude
other space-like characters such as tabs, you'll have to modify this
expression.)
2. Text may or may not contain slashes.

I'm not sure why this condition is even listed. It's okay to have slashes,
but it's also okay not to have them. This condition is meaningless.

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

Jussi Piitulainen

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

Jussi Piitulainen

Mark said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top