Results 1 to 8 of 8
  1. #1
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

    Default Question about regular expressions

    Hello, I am currently working on regular expressions in the form 'makeStuff(stuff)'. I have console in which the text is checked to see if it conforms to the grammer. So basically if I want to make a flower in a graphics program, I say makeStuff(flower) in the console and it is done.
    This works just fine. I was just wondering how I can get my grammer to allow a number to be followed by the object I wish to create. In other words if I want to make 2 flowers appear on screen, I would like to be able to say "makeStuff(flower,2)" to make two flowers appear on screen. My current regular expression looks like this:
    Java Code:
    public static final String commandPattern = "(\\w+)\\((\\w+)\\)";
    This allows for any number of words followed by an '(' any number of characters followed by ')'
    My question is how can I make it so I have the same thing but have any number of characters followed by '(' any number of characters followed by ')' followed by ',' followed by an int followed by ')' ?
    An example would be great on how to do this.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Question about regular expressions

    You can combine it into one pattern like this.
    Java Code:
     public static final String commandPattern = "(\\w+)\\((\\w+)(?:,(\\d+))?\\)";
    Unfortunately, the capture group for the count will still be there and will be null if not present. If you want to use the
    groupCount directly after a successful match and not worry about null handling, then you need a separate pattern like this:

    Java Code:
       public static final String commandPattern2 = "(\\w+)\\((\\w+),(\\d+)\\)";
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

    Default Re: Question about regular expressions

    Thanks that helps me out a lot

  4. #4
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

    Default Re: Question about regular expressions

    One more quick question. Can I make the number after the object optional? If I only want a command to act on only one object I wouldn't want to have to say doSomething(stuff,1). Instead I would rather just want to say doSomething(stuff), to avoid being redundant (when no number is specified I will make the default value of the object 1). Can I do that? If so how might that look?
    Last edited by Snake006; 10-08-2014 at 03:17 AM.

  5. #5
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

    Default Re: Question about regular expressions

    Of course this
    public static final String commandPattern = "(\\w+)\\((\\w+)(?:,(\\d+))?\\)";
    seems to do that, at least I remember something about the ? making something optional. I guess I don't mind if the number is made null if no number is provided.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Question about regular expressions

    Another way is to use two patterns like I suggested. If one matches, then just use that group count. If it doesn't match, then try the other
    pattern. If that matches, then use that group count.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

    Default Re: Question about regular expressions

    I guess I answered my own questioned, I forgot that the ? makes something optional(I just tested it). So now I just need to implement all that in a meaningful way.
    Right now I am using what is below to handle a command and an object or argument. :
    Java Code:
    if(dic.isCommand(m.group(1)) && dic.isArg(m.group(2))){
                    doCommand(m.group(1),m.group(2));
    My final question is how I might identify that optional 3rd group, which will of course be a number. I probably can then run the whole string by that other regular expression, however I am currently unsure how to do that(I wish I could say something like m.groupCount>2 then do a multiple command or something like that). I may figure this out by the time someone comes with a reply but until then any thoughts?

  8. #8
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

    Default Re: Question about regular expressions

    I think that I am getting the hang of it. I made a is multCommand() method that return true if group count(3) is not null. Seems to be working fine now. Once I determine if its null or not I should be able to easily proceed from there

Similar Threads

  1. java regular expressions
    By alexandra12 in forum New To Java
    Replies: 4
    Last Post: 08-28-2013, 01:09 PM
  2. Regular expressions
    By freelancer in forum New To Java
    Replies: 1
    Last Post: 11-25-2011, 12:41 PM
  3. Regular Expressions Help
    By Death Sickle in forum New To Java
    Replies: 4
    Last Post: 04-04-2011, 04:21 AM
  4. Regular Expressions problem
    By mohammedelbes in forum Advanced Java
    Replies: 7
    Last Post: 01-05-2011, 11:12 AM
  5. regular expressions
    By sozeee in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 09:58 PM

Posting Permissions

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