Results 1 to 4 of 4
  1. #1
    sgtblitz is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Java Regular Expressions: Comma Seperated List

    Hi there guys,

    I'm new to these forums but basically I have a little query about regular expressions.

    Basically I have a project where I load in a text file containing an unlimited length of comma separated list of strings where the last string doesn't have a comma at the end of it.

    e.g.:
    johnny,matinay,crazy,rampage,eastpak,ghghg

    All I want to do is to create a regular expression that will ensure that there is a comma separated list of strings where in each string there are alphanumeric characters, the "&" sign and white space but NO commas inside the string (since that is what the list is split with), so if the above list was changed to "johnny,matinay,,rampage,eastpak,ghghg" where there is one comma straight after the other then the regular expression would pick this up and throw an error.

    All I have so far is:
    [^,]+[,].+

    But i'm not sure where to go from there.

    Thanks in advance guys!

    [^,]+[,].+

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,862
    Rep Power
    8

    Default

    Why don't you just check for the substring ",,"? Or just check whether one of the matches is an empty String?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Java Code:
    public class NoDoubleComma {
    
      public static void main(String[] args) {
        String[] inputs = {
          "johnny,matinay,crazy,rampage,eastpak,ghghg",
          "johnny,matinay,,rampage,eastpak,ghghg"
        };
        String regex = "^([\\p{Alnum}&\\s]+,)+[\\p{Alnum}&\\s]+$";
        for (String input : inputs) {
          if (input.matches(regex)) {
            System.out.print("Matches:       ");
          } else {
            System.out.print("Doesn't match: ");
          }
          System.out.println(input);
        }
      }
    }
    Description of regex:
    ^ matches the start of input
    ( starts a group
    [\p{alnum}&\\s] character class that matches alphanumeric characters, the character '&' and white space.
    , matches a comma
    )+ matches the part inside the parentheses, repeated one or more times
    [\p{alnum}&\\s] As before, after the last comma
    $ matches end of input

    db

  4. #4
    sgtblitz is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Java Code:
    public class NoDoubleComma {
    
      public static void main(String[] args) {
        String[] inputs = {
          "johnny,matinay,crazy,rampage,eastpak,ghghg",
          "johnny,matinay,,rampage,eastpak,ghghg"
        };
        String regex = "^([\\p{Alnum}&\\s]+,)+[\\p{Alnum}&\\s]+$";
        for (String input : inputs) {
          if (input.matches(regex)) {
            System.out.print("Matches:       ");
          } else {
            System.out.print("Doesn't match: ");
          }
          System.out.println(input);
        }
      }
    }
    Description of regex:
    ^ matches the start of input
    ( starts a group
    [\p{alnum}&\\s] character class that matches alphanumeric characters, the character '&' and white space.
    , matches a comma
    )+ matches the part inside the parentheses, repeated one or more times
    [\p{alnum}&\\s] As before, after the last comma
    $ matches end of input

    db
    Thanks so much my friend :) Works perfectly!

Similar Threads

  1. Regular Expressions Help
    By Death Sickle in forum New To Java
    Replies: 4
    Last Post: 04-04-2011, 04:21 AM
  2. How to create regular expressions in Java
    By maz09 in forum New To Java
    Replies: 12
    Last Post: 04-02-2010, 05:13 PM
  3. Replies: 2
    Last Post: 11-22-2009, 05:24 PM
  4. Replies: 3
    Last Post: 11-21-2009, 05:48 PM
  5. Regular Expressions in java
    By blue404 in forum Advanced Java
    Replies: 2
    Last Post: 09-26-2008, 03:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •