SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Regex help

Hybrid View

  1. #1
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regex help

    I'm making a regex to validate money data, and these are the rules I want: (X will represent numbers)

    $ must be at front
    .XX -OR- .X is optional for end
    . at end NOT valid
    1-3 digits left of decimal
    infite combinations of XXX, where needed

    Examples of valid formats
    $xx,xxx.xx
    $xxx,xxx,xxx,xxx
    $x,xxx,xxx.x
    $x.xx

    Examples of invalid formats:
    $xxxxx
    xx,xxx.xx
    $xx,xxx.
    $xxx,xxx.xxx

    Here is what I've got so far, but I know it doesn't work...
    Code:
    moneyReg = "^\\$([0-9]{1,3}[,]{1})*([0-9]{1-3})+(\\.[0-9]{1,2})?$";
    I've had several iterations that have had some success, but not total. This is where I am now, and it breaks pretty much on everything...my brain is too tired to backtrack.

    Thanks in advance!
    Last edited by beetle; Jul 23, 2002 at 10:12.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking Nevermind!

    Sorry folks...just figured it out
    Code:
    moneyReg = "^\\$(?:(?:[0-9]{1,3},)(?:[0-9]{3},)*[0-9]{3}|[0-9]{1,3})(\\.[0-9]{1,2})?$";
    Meep, that's a BIG one!
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  4. #4
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    \d can replace the [0-9] sections... can't it?

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure can, but since I'm declaring this regex in a string, I like to avoid as many backslashes as possible, as each one has to be escaped by another backslash. Sounds picky, but starts to look confusing...much easier to see the atoms as [0-9] for me.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why declare it in a variable?

    still two more characters each time - but that's your preference

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, for what I'm using this for, some of the values in the regex come from varialbes. Easier to concatenate them as a string first, and then create a new regex object from the string. Like input that must be a number, variable in length
    Code:
    var lBound = 2;
    var uBound = 6;
    
    var numReg ="/^[0-9]{"+lBound+","+uBound+"}$/";
    var regex = new RegExp(numReg);
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  8. #8
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Makes sense now

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---


Bookmarks

Posting Permissions

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