Thread: (.*?) or (.*)

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

    Join Date
    Apr 2003
    Posts
    342
    Rep Power
    12

    (.*?) or (.*)


    I've seen people use either (.*?) or (.*) for wild, what is the difference?
  2. #2
  3. Recovering Intellectual
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2006
    Location
    Orange County, CA
    Posts
    1,306
    Rep Power
    785
    dot means match everything, star means match one or more times.

    question mark means the match is optional.

    I dont really know why someone would use '.*?' when they wanted '.*'
    Bugs that go away by themselves come back by themselves
    Beware - your loyalty will not be rewarded
  4. #3
  5. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    Not quite.

    Originally Posted by Matt1776
    dot means match everything,
    Dot means match any character except new line characters.

    Originally Posted by Matt1776
    star means match one or more times.
    Star means zero or more times.

    Originally Posted by Matt1776
    question mark means the match is optional.
    Right after a greedy operator (like STAR or PLUS) the question mark is a reluctant quantifier (aka a "ungreedy" quantifier).

    Originally Posted by Matt1776
    I dont really know why someone would use '.*?' when they wanted '.*'
    Here's a good reason why: http://www.regular-expressions.info/repeat.html (see the paragraph "Watch Out for The Greediness!")

    Comments on this post

    • Matt1776 agrees : Thank you for the clarification.
    • Sarah_S agrees
  6. #4
  7. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    Originally Posted by tommy916
    I've seen people use either (.*?) or (.*) for wild, what is the difference?
    Check out the link from my previous reply.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    45
    Rep Power
    20
    Originally Posted by tommy916
    I've seen people use either (.*?) or (.*) for wild, what is the difference?
    Non-Greedy matches are usually better due to most people use a greedy match such as:

    /^staticstring(.*)anotherstaticstring$/

    With the above the regex engine will do a ton of back referencing/tracking to find where "a" from "anotherstaticstring" occurs to know where to cut off the greedy matching.

    By doing (.*?) if changes the way it looks for the start of the literal match.

    The same approach works for something like (\d+?) I believe as well.

    Correct me if i'm wrong but this has been my experience in my everyday battles with regex.
  10. #6
  11. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    Originally Posted by xyexz
    ...

    The same approach works for something like (\d+?) I believe as well.

    Correct me if i'm wrong but this has been my experience in my everyday battles with regex.
    An in my opinion better explanation is give in the link I posted earlier.
  12. #7
  13. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    Even setting aside the greedy v. non-greediness of those constructs, it's worth pointing out that using dot-star is not necessarily recommended if you can make your regexp tighter.

IMN logo majestic logo threadwatch logo seochat tools logo