Thread: Reg-ex Needed

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

    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Reg-ex Needed


    Hi Friends,

    Need reg-ex for the following scenario,

    Scenario: www/a/b/page.go?qs=a&qs=b

    Required: www/a/b/page.go

    But the regex should work in both conditions either it has query strings or not.

    I tried .*\? but this fails in second condition. Please help me friends.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,958
    Rep Power
    9397
    String equals "www/a/b/page.go" or string starts with "www/a/b/page.go?". No regular expressions needed.

    For the record,
    Code:
    #^www/a/b/page\.go($|\?)#
    "Starts with that path and immediately following is (the end of the string or a question mark)"
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by requinix
    String equals "www/a/b/page.go" or string starts with "www/a/b/page.go?". No regular expressions needed.

    For the record,
    Code:
    #^www/a/b/page\.go($|\?)#
    "Starts with that path and immediately following is (the end of the string or a question mark)"
    Thx for ur regex but this URL - www/a/b/page\.go is not constant. It may keeps on changing.

    Like,

    www/a/b/page
    www/a/b/page.go
    www/a/page
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,958
    Rep Power
    9397
    Everything I said still applies.

    If you do take the regex route, which I don't advise, then make sure you escape the string before sticking it in the expression.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by requinix
    Everything I said still applies.

    If you do take the regex route, which I don't advise, then make sure you escape the string before sticking it in the expression.
    I altered slightly, Yeah it works for this condition alone,

    .*go($|\?)

    www/a/b/page.go

    How about the below ones there is no end matches,
    www/a/b/page?
    www/a/page
  10. #6
  11. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,958
    Rep Power
    9397
    Your altered version won't work then. Naturally. So how about... not altering it?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by requinix
    Your altered version won't work then. Naturally. So how about... not altering it?

    Its also not working
  14. #8
  15. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,958
    Rep Power
    9397
    What is the code you're using and what are the input strings that are failing?
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    I think that you need to be a bit more specific on what you want to match (and what you don't want to match).
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by Laurent_R
    I think that you need to be a bit more specific on what you want to match (and what you don't want to match).
    This is my requirement,
    Posibilities:

    www.a.com/a/b/c.go?qs=a;qs=b
    www.a.com/a/b/c
    www.a.com/a/c.go

    Requiredwithout query string)

    www.a.com/a/b/c
    www.a.com/a/c.go
  20. #11
  21. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    Hi,

    so you wanna cut off the possible query part from an arbitrary URL?

    Code:
    ^[^?]+
    But like always, regexes are the wrong tool for this. Whatever language you're using, it most definitely has a URL parser (either built-in or through a library) for tasks like this. You know, you're not the first to work with URLs. No need to reinvent the wheel.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by Jacques1
    Hi,

    so you wanna cut off the possible query part from an arbitrary URL?

    Code:
    ^[^?]+
    But like always, regexes are the wrong tool for this. Whatever language you're using, it most definitely has a URL parser (either built-in or through a library) for tasks like this. You know, you're not the first to work with URLs. No need to reinvent the wheel.
    yeah thx Jacques the code works as i expected.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    10
    Rep Power
    0
    ([a-z / .]+)\? this would select all the similar patterns
  26. #14
  27. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    Originally Posted by klipangel
    ([a-z / .]+)\? this would select all the similar patterns
    Not really. What about double colons (for the port), numbers, dashes, underscores, tildes, percent-encoded characters, internationalized domains?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo