Regular Expression

Discussion in 'Java' started by Rahul, Nov 8, 2006.

  1. Rahul

    Rahul Guest

    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
    Rahul, Nov 8, 2006
    #1
    1. Advertising

  2. Rahul

    Daniel Pitts Guest

    Rahul wrote:
    > 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

    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.
    Daniel Pitts, Nov 8, 2006
    #2
    1. Advertising

  3. Rahul

    John Gordon Guest

    In <> "Rahul" <> writes:

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

    --
    John Gordon "... What with you being his parents and all,
    I think that you could be trusted not to shaft
    him." -- Robert Chang, rec.games.board
    John Gordon, Nov 8, 2006
    #3
  4. Rahul

    Oliver Wong Guest

    "John Gordon" <> wrote in message
    news:eitahe$it7$...
    > In <> "Rahul"
    > <> writes:
    >
    >> 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
    Oliver Wong, Nov 8, 2006
    #4
  5. Rahul

    Rahul Guest

    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 Gordon wrote:
    > In <> "Rahul" <> writes:
    >
    > > 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.
    >
    > --
    > John Gordon "... What with you being his parents and all,
    > I think that you could be trusted not to shaft
    > him." -- Robert Chang, rec.games.board
    Rahul, Nov 9, 2006
    #5
  6. Rahul

    Mark Guest

    Rahul wrote:
    > 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 Gordon wrote:
    > > In <> "Rahul" <> writes:
    > >
    > > > 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.
    > >
    > > --
    > > John Gordon "... What with you being his parents and all,
    > > 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.
    Mark, Nov 9, 2006
    #6
  7. 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.
    Jussi Piitulainen, Nov 9, 2006
    #7
  8. Mark writes:

    > 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, Nov 9, 2006
    #8
  9. 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.h...websp-20&linkCode=ur2&camp=1789&creative=9325

    Mastering Regular Expressions, Second Edition

    Cheers!

    -Cameron McKenzie


    Rahul wrote:
    > 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
    www.pulpjava.com, Nov 9, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith-Earl
    Replies:
    1
    Views:
    441
    Mary Chipman
    Jun 15, 2004
  2. VSK
    Replies:
    2
    Views:
    2,269
  3. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    831
    Alan Moore
    Dec 2, 2005
  4. GIMME
    Replies:
    3
    Views:
    11,921
    vforvikash
    Dec 29, 2008
  5. Noman Shapiro
    Replies:
    0
    Views:
    219
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page