#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    233
    Rep Power
    76

    A regex for two numbers separated by a space or comma


    Hi, i would like to create a regex match for X Y coordinates.

    X and Y can be positive or negative; decimal or integer.

    They are separated by a space, comma, or tab.

    This is what I have (code below), and it works, BUT the problem is that it allows more than two numbers.

    For instance, 1,1 works, which is good, but 1,1,1,1,1,1 also works, which is bad.

    I have the \b (boundary) tags on each side to signify the start and end of the string, but I guess its not working.

    By the way, the programming language is VB.NET if that makes any difference. If you can help me, I will be most appreciative.

    My code:
    Code:
    Dim strRegEx As String = "\b(-{0,1}\d*\.{0,1}\d+)[, \t](-{0,1}\d*\.{0,1}\d+)\b"
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    \b matches the beginning or end of words. ^ and $ match the beginning and end of the string (respectively).

    Incidentally, ? has the same meaning as {0,1} and is a bit easier to read.

    Comments on this post

    • latros agrees
    • ishnid agrees
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    233
    Rep Power
    76
    perfect! Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo