Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Regular Expression

Thread view: 
Rahul - 08 Nov 2006 18:57 GMT
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
Daniel Pitts - 08 Nov 2006 19:06 GMT
> I need a regular expression..
> The conditions are as follows...
[quoted text clipped - 3 lines]
>
> Thanking you
First. We won't do your homework.
Second. This doesn't look like a java question.
Third.
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
will tell you a little about regex.
Fourth. Try google for regex. You'll get a faster turnaround.
John Gordon - 08 Nov 2006 19:18 GMT
> 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.

Signature

John Gordon             "... What with you being his parents and all,
gordon@panix.com         I think that you could be trusted not to shaft
                        him."  -- Robert Chang, rec.games.board

Oliver Wong - 08 Nov 2006 20:25 GMT
>> I need a regular expression..
>> The conditions are as follows...
[quoted text clipped - 13 lines]
> 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
Rahul - 09 Nov 2006 10:21 GMT
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.

> > 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
Mark - 09 Nov 2006 10:30 GMT
> 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.
Jussi Piitulainen - 09 Nov 2006 11:08 GMT
...
> 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.
www.pulpjava.com - 09 Nov 2006 17:02 GMT
Regular expressions will save your life, IF you know how to use them.

Grab this book on regular expressions. It will help you out
tremendously:

http://www.amazon.com/gp/redirect.html?ie=UTF8&location=http%3A%2F%2Fwww.amazon.
com%2FMastering-Regular-Expressions-Second-Jeffrey%2Fdp%2F0596002890%2Fsr%3D8-1%
2Fqid%3D1163091624%3Fie%3DUTF8%26s%3Dbooks&tag=pulpjavawebsp-20&linkCode=ur2&cam
p=1789&creative=9325


Mastering Regular Expressions, Second Edition

Cheers!

-Cameron McKenzie

> I need a regular expression..
> The conditions are as follows...
[quoted text clipped - 3 lines]
>
> Thanking you


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.