RegEx library question

49 views
Skip to first unread message

Mario Barón

unread,
Aug 15, 2014, 10:32:04 AM8/15/14
to codenameone...@googlegroups.com
Hey guys,

I have a question about the regEx library, I'm really just a beginner with reguar expressions but I've been trying to write one that helps me evaluate when the user writes a home address which should be in this format (for example):

Street 3 # 6 - 4

it should check if the user wrote something from the # character onwards including the #. I have tested this expression /(?:# *).+/gi in some online testers like this one and it seems to do the trick. But when I run it through the regEx library it always returns false as if no matches were found. 

This is my code by the way:

 public static boolean validateRegEx(String pattern, String source) {
    RE r = new RE(pattern);
    if (r.match(source)) {
      return true;
    } else {
      return false;
    }
  }

Is there something wrong with the expression, or is there something I'm not doing right with the library? Really appreciate your help.


Thanks.

Steve Hannah

unread,
Aug 15, 2014, 10:35:59 AM8/15/14
to codenameone...@googlegroups.com

Can you post the part of the code where you specify the regex string.  Make sure not to include the slashes at the beginning and end .

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.
Visit this group at http://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/7404416f-0a4e-46e4-bdf5-81293da5188c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mario Barón

unread,
Aug 15, 2014, 10:40:14 AM8/15/14
to codenameone...@googlegroups.com
Hey Steve,

thanks for the extremley fast response =) here is the full code:

  public static final String DIRECTION_PATTERN = "/(?:# *).+/gi";
  String direction = "Carrera 32 # 49-44";

 boolean isDirectionComplete = Utilities.validateRegEx(Utilities.DIRECTION_PATTERN, direction);

Mario Barón

unread,
Aug 15, 2014, 10:41:48 AM8/15/14
to codenameone...@googlegroups.com
Sorry, accidentally published that incomplete. I have tried with 

direction = "Carrera 32";

also and it still says it doesn't match.


On Friday, August 15, 2014 9:32:04 AM UTC-5, Mario Barón wrote:

Steve Hannah

unread,
Aug 15, 2014, 10:56:53 AM8/15/14
to codenameone...@googlegroups.com
Regex strings should be java-style regex strings.  You don't include the '/' at the beginning and end.  And your "gi" flags should be specified using the match flags in the second parameter.
These javadocs should be helpful.

So your regex would be something like:
RE re = new RE("(?:# *).+", RE.MATCH_CASEINDEPENDENT);

Steve


--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.
Visit this group at http://groups.google.com/group/codenameone-discussions.

For more options, visit https://groups.google.com/d/optout.



--
Steve Hannah
Web Lite Solutions Corp.

Mario Barón

unread,
Aug 15, 2014, 12:12:47 PM8/15/14
to codenameone...@googlegroups.com
Ok that seemed to work.  Thanks a lot Steve. 



On Friday, August 15, 2014 9:32:04 AM UTC-5, Mario Barón wrote:

Mario Barón

unread,
Aug 15, 2014, 5:33:50 PM8/15/14
to codenameone...@googlegroups.com
Hey Steve, 

sorry for bothering you again, but I'm testing this regular expression and it only seems to validate the address as incomplete when it doesn't contain blank spaces after the # character. Do you know how I can modify it to take this case into consideration? I was sure the * after the space included this case but as I said previously, I'm not that experienced with regular expressions

For example:

address= "Carrera 23"   ==> incomplete
address= "Carrera 23 #"   ==> incomplete
address= "Carrera 23 # "   ==> incomplete
address= "Carrera 23 #         "   ==> incomplete
address= "Carrera 23 # 31a"   ==> complete
address= "Carrera 23 #31a"   ==> complete


Thanks a million.


On Friday, August 15, 2014 9:32:04 AM UTC-5, Mario Barón wrote:

Steve Hannah

unread,
Aug 18, 2014, 5:58:42 PM8/18/14
to codenameone...@googlegroups.com
If you want to just match the # to the end of the input, you could just to
RE re = new RE("#.*$", RE.MATCH_CASEINDEPENDENT);

Steve


--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.
Visit this group at http://groups.google.com/group/codenameone-discussions.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages