Regular Expressions (PERL subset); greedy/non-greedy

Discussion in 'other software & services' started by FanJ, Nov 24, 2003.

Thread Status:
Not open for further replies.
  1. FanJ

    FanJ Guest

    Hi,

    Could anyone tell me exactly the meaning/syntax of greedy/non-greedy.

    Programs like MailWasher and BeyondCompare use a subset of the PERL Regular Expressions.
    That is why I am asking this.

    Somehow I have the feeling it has to do with the mathematical "or", but I might be wrong here :rolleyes:

    I am familiar with the mathematical "or":

    A v B
    Pronounced as: A or B
    Meaning in every day language:
    A or B or both
    which means A or B or (A and B).

    Is greedy/non-greedy related to this?
    Somehow like:
    one meaning: A or B or both
    the other meaning: A or B but NOT both.

    I have to admit that I took here the easy way:
    asking before I took the time to look deeper at it :oops:
    I'm getting old :rolleyes:

    Unicron maybe? ;)
     
  2. FanJ

    FanJ Guest

    Oops, I know I'm asking a lot....

    With "exactly" in the previous posting, I did mean that.
    Both in every day language as in the right syntax.

    :rolleyes: :oops: :rolleyes: Oops, I know I'm asking a lot...... ;)
     
  3. Dan Perez

    Dan Perez Retired Moderator

    Joined:
    May 18, 2003
    Posts:
    1,495
    Location:
    Sunny San Diego
    Hi Jan ;)

    Greedy is a mode of pattern matching where the match will be applied as much as possible on the search set (for instance the line) whereas non-greedy will only match the minimum or first instance of a match

    so your understanding is correct but the implication is more wide reaching then your examples.

    for instance in greedy mode if you do a search for <.*> on a line such as

    <html><p>Hello there</p></html>

    then the entire line will be returned as the entire text can be seen to be within two matching angle brackets. but if you were to do the same test using non-greedy mode it would take the smalles matching subset of the line which would be

    <html>

    Hope this helps!

    Dan
     
  4. FanJ

    FanJ Guest

    Heya Dan my friend :)

    Thanks a lot :)
    Karma cookie going your way :D

    It certainly helped me !!

    You know, I'm a very strange guy with that one-way mind :rolleyes: (Happens sometimes to us mathematicians :rolleyes: ).
    When I had to write a SPSS program over 30 years ago, I took about a month to read the manual from start to finish, hum.... :rolleyes:
    Darn math-folks who always want to know the syntax and meaning :rolleyes: :oops:
     
Thread Status:
Not open for further replies.