SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist Alchemist's Avatar
    Join Date
    Aug 2006
    Posts
    412
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    REGEXP count property

    hello,

    can someone tell me why the following code incorrectly returns 2 as matches count whereas when I change str = "179,80,179,80,179" I get the correct count which is 3? thanks

    str = "179,179,179"
    num = 179

    Set regEx = New RegExp
    regEx.Global = true
    regEx.IgnoreCase = True
    regEx.Pattern = "(^|,)" & num & "($|,)"

    Set colMatches = regEx.Execute(str)
    response.write colMatches.Count & "<BR>"

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm no regex expert but I think you want to force "non greedy matching", so try this:

    regEx.Pattern = "(^|,)?" & num & "($|,)?"

  3. #3
    SitePoint Evangelist Alchemist's Avatar
    Join Date
    Aug 2006
    Posts
    412
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you jimfraser....I have one more question: I want to remove from a string any instances of and/or repeats and replace them with the preceding reference. kind of like this:

    regEx.Pattern = "(\s)(and|or)(\s)(and|or)(\s)"
    str = Trim(regEx.Replace(str, " $2 "))

    in a string like "dog and or cat or fish" , it should just replace the first " and or " block with " and ". however, this is not working out.....

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand.

    str = Replace(str, "and or", "and")

    ?

  5. #5
    SitePoint Evangelist Alchemist's Avatar
    Join Date
    Aug 2006
    Posts
    412
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jimfraser
    I don't understand.

    str = Replace(str, "and or", "and")

    ?
    that is just an example. it can be any combinations of and|or which is unknown since it's coming from user input. also, it has to replace ALL occurences of and|or repeats. for example:

    str = "dog AND OR cat OR fish" ---> dog AND cat OR fish
    str = "dog AND OR AND cat OR fish" ---> dog AND cat OR fish
    str = "dog OR AND cat OR fish" ---> dog OR cat OR fish
    str = "dog AND AND cat OR fish" ---> dog AND cat OR fish

    in other words, I have to validate a search input so that inserted keywords are separated either by a space by ONE operator only (AND,OR)

    thanks


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
  •