Consulting

Results 1 to 5 of 5

Thread: Solved: Modifying regex to accept comma's?

  1. #1
    Site Admin VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,008
    Location

    Solved: Modifying regex to accept comma's?

    Hi all i'm using regex (provided for me by some one else because i don't have the foggiest with it) to force an entry of letters (at least for the first two, rather than all numeric, however i want the user to be able to enter the data in this fashion xxxxxx, xxxxxx where x will be a character, however, using this ^[a-z]{2}[a-z ]*$ does not allow the comma, can someone help modify it to accept it?
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,447
    Location
    Psst!!!

    In the unlikely event you didn't get your answer here try The Code Cage!
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    Site Admin VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,008
    Location
    Ted, now why didn't i think of that?
    Now also posted here: http://www.thecodecage.com/forumz/ex...tml#post395094
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi Simon,

    This could be off, but I think may be okay (how's that for confidence?). Interested but haven't used RegExp, so was giving it a spin since you posted.

    Anyways, I see that you came up with something at your link. I'm not sure if important to what you are currently doing, but leastwise in this testing - your current appears to allow multiple commas and/or multiple spaces and/or spaces between strings w/no commas. In case that would goober up a search or split or anything, here is where I got on it.
    [vba]
    Private Sub Worksheet_Change(ByVal Target As Range)
    '// Early-bound: Microsoft VBScript Regular Expressions 5.5

    Dim RegExpress As New RegExp

    Application.EnableEvents = False
    If Target.Count = 1 Then
    If Not Target.Value = vbNullString Then
    With RegExpress
    '// 2 or more alpha char, followed by 0 to 1 comma(s),
    '// followed by 0 to 1 spaces, repeatable 1 to x times,
    '// I uh...think?
    .Pattern = "^([A-Za-z]{2,}[\,]?[ ]?){1,}$"
    ' "^[A-Za-z]{2}[,A-Za-z ]*$"
    If Not .test(Target) Then
    Target.Value = vbNullString
    End If
    End With
    End If
    End If
    Application.EnableEvents = True
    End Sub
    [/vba]

    Hope that helps (heck, I hope it doesn't blow up anything),

    Mark

  5. #5
    Site Admin VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,008
    Location
    GTO, thanks for going to the effort, thats pretty good, far to complex for my situation as i do indeed need the ability for multiple comma's and spaces, but thats a pretty useful routine
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

Posting Permissions

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