Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

REGEX Simple Expression Question

Just a quick curious question. Before I knew regex I did some research on how I can pull information from the source of webpages and found that people would use:

PHP Code

<?php 
somestuff
(.*?)somemorestuff 

Which was used to pull all information between two sets of strings.

Now that I'm learning regex I know that the "?" refers to a character being "optional". So if I have:

PHP Code

<?php 
abc
ac 

And I want to select both lines I'd use:

PHP Code

<?php 
ab
?c 

This leads me to my actual question... if I want to select everything between "abc" in this string:

PHP Code

<?php 
abc dsfljkdslkfjdslfkj abc 

What's wrong with

PHP Code

<?php 
abc
(.*)abc 

Why would I need "?" in the expression? This makes zero sense to me.
[Image: 5BgQiIm.gif]

.* will match as much as possible and is Greedy, while .*? will match only as much as is needed because the ? changes Greedy to Stingy.

"foo.*bar" This will match "food is under the bar in the bar" - Greedy
"foo.*?bar" This will match "food is under the bar" -

Try to put in abc somewhere in your string and test the difference
(abc dsfljkdsabclkfjdslfkj abc)

Hoppfully you understood me, it's way too early to think straight..
NEVERMIND:

If I had another "abc" in the middle of my data ".*" means match all of any character. So this would only stop at the last occurrence of my line.

Adding the ".*?" in simple terms means match all of any character and "not be greedy". So the first occurrence makes regex stop there instead of the new line.


(Aug 23, 2017 10:11 PM)Vanagander Wrote:  .* will match as much as possible and is Greedy, while .*? will match only as much as is needed because the ? changes Greedy to Stingy.

"foo.*bar" This will match "food is under the bar in the bar" - Greedy
"foo.*?bar" This will match "food is under the bar" -

Try to put in abc somewhere in your string and test the difference
(abc dsfljkdsabclkfjdslfkj abc)

Hoppfully you understood me, it's way too early to think straight..

You got here right when I figure it out lol.

But thank you for your answer! I looked at your response and it did clarify a little more for me. Thank you 🤝
[Image: 5BgQiIm.gif]



Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Poojar102Nov 1, 2021 10:32 AM 4 187 Nov 1, 2021 10:32 AM
Last Post: Maquina
[Python] by Wic12Sep 23, 2019 09:27 PM 1 166 Sep 23, 2019 09:27 PM
Last Post: datguy.dev
[C#] by GraeskMytologiApr 6, 2019 09:55 AM 1 156 Apr 6, 2019 09:55 AM
Last Post: 0xDEAD10CC