/    Sign up×
Community /Pin to ProfileBookmark

Explanation of (.*?) in Regular Expressions

Hello all,

I’ve seen the following code in xbloglite:

[code=php]$entry = preg_replace(“/[b](.*?)[/b]/i”,”<strong>$1</strong>”,$entry);[/code]

Now, I know how to implement this, and it has been very useful- but I don’t really understand how it works, and haven’t seen an explanation of it in any RegEx tutorial.

Of course, I only read the simple things, and this is probably a combination of more advanced techniques (which are beyond me), but I would still like to know how it works exactly, seeing as I’m only writing PHP to learn.

Anyone who has the patience enough with my newbishness to help me out here, I thank in advance. ?

EDIT: Realized I didn’t directly state that I understood the word boundary, //, or the i after the word boundary to make it disregard about the case of the text string contained within (as much of a technical definition as I can offer).

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@pyroJul 11.2004 — First of all, since these boards mess up the code when you post it like that, I'll repost it, so others can see what we're talking about...

$entry = preg_replace("/[b](.*?)[/b]/i","&lt;strong&gt;$1&lt;/strong&gt;",$entry);

Here's the step-by-step...

First, we are matching a literal &#91;b&#93;. We need the backslashes in front of the [ and ], to escape them, as they have special meaning in regex (to denote a character set). Next, the parenthesis are used to remember the match. . means to match any character, and * means 0 or more times. The ? is used to make the matching ungreedy. Finally the [/b] is used to match &#91;/b&#93;. Again, the backslashes are used to escape the regex special characters.

Let me know if you still do not understand it and I'll try to explain better. ?
Copy linkTweet thisAlerts:
@Ben_RogersauthorJul 11.2004 — I understand what you've said so far, about what the (.*?) represents... but how is $1 created? I want to think it's created by the "?" because that reminds me of query strings, but I'm not sure- and it may come in handy later to fully understand this.

<brown-nosing type="slightly-offtopic">

Thank you for commenting xBlogLite- I've been able to get a lot of inspiration from it. The main reason I'm creating my own blog is because I want to have more control over the comments form, and for practice.

</brown-nosing>

EDIT: Typos, typos, typos...
Copy linkTweet thisAlerts:
@pyroJul 11.2004 — The parenthesis are what tell the regex engine to remember the match. Basically, it will match anything until it finds a &#91;/b&#93; (due to the ungreediness of the match - the ?) and remember the match. So to reiterate, the ? has nothing to do with the matching, but rather has to do with the greediness of the quantifier.

As far as commenting the code - I'll be as happy as you are when I go back trying to remember why I did something. ?
Copy linkTweet thisAlerts:
@Ben_RogersauthorJul 11.2004 — Alright, now I get you, it remembers it and is available to you in variable format. I may've misinterpreted you the first time, but now I completely understand how that works. Thanks again. ?

Ah, true, true- but I have commented my scripts and I STILL don't know why I did some things... but I guess that goes back to my newbishness. (I deleted a two paragraph rant about old scripts here... best not to post those.) ?
×

Success!

Help @Ben_Rogers spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...