₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,331,105 members, 8,448,670 topics. Date: Monday, 20 July 2026 at 04:50 PM

Toggle theme

Regex Gurus Please Help! - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingRegex Gurus Please Help! (1300 Views)

1 Reply (Go Down)

Regex Gurus Please Help! by webninja(op): 1:48pm On Nov 21, 2015
Please, i'm learning regex, i don't really understand this expression e.g to replace image: <img src="http://google.com/image.jpg"/> with bbcode:
/\[img\](.+?)\[\/img\]/ig

Please, what does the (.+?) mean? explain to me bit by bit with examples, and why $1 coming as a variable? And i see $2 sometimes in some expressions... Your goodness of heart will be appreciated thank you.
Re: Regex Gurus Please Help! by Nobody: 6:30pm On Nov 21, 2015
What (.+?) Means is 'Match any character that comes btween the [img] one or more times'

. Signifies any character

+ signifies one or more of the previous characters

? Is match the previous character if there is a match I.e if + had a match ..else it would not match anything).



The variable,$1 is used in saving the matched word from the regex

$2 ___ same thing as above
Re: Regex Gurus Please Help! by webninja(op): 6:50pm On Nov 21, 2015
Jregz:
What (.+?) Means is 'Match any character that comes btween the [img] one or more times'

. Signifies any character

+ signifies one or more of the previous characters

? Is match the previous character if there is a match I.e if + had a match ..else it would not match anything).



The variable,$1 is used in saving the matched word from the regex

$2 ___ same thing as above
Thank you very much.
Then what about the brackets (.+?)
Re: Regex Gurus Please Help! by Nobody: 4:49am On Nov 22, 2015
webninja:
Thank you very much.
Then what about the brackets (.+?)
Brackets are used to capture texts from a pattern in other to apply it's mached result (those in the parenthesis) to the rest of the regex..you could have a regex that wants to match the word 'clown' (or any other word) four times,you enclose the first regex that searches for a string in a parenthesis then apply the text mathed to the rest of the regex (the same regex)..they are usually available in \1 and so on
Re: Regex Gurus Please Help! by webninja(op): 1:56pm On Nov 22, 2015
Jregz:
Brackets are used to capture texts from a pattern in other to apply it's mached result (those in the parenthesis) to the rest of the regex..you could have a regex that wants to match the word 'clown' (or any other word) four times,you enclose the first regex that searches for a string in a parenthesis then apply the text mathed to the rest of the regex (the same regex)..they are usually available in \1 and so on
Ok, thanks man, now how do the $2 come in? Please show some code example smiley
Re: Regex Gurus Please Help! by Nobody:
webninja:
Ok, thanks man, now how do the $2 come in? Please show some code example smiley
$2 means that the regex machine should capture the 2nd group in the String

Example
String ="I amam brotherbrother";

Regex Pattern to match the repeated String would be:

(\w+)$1\s(\w+)$2

Explanation

The first (\w+) would match [am]
Then $1 would also match the next [am].

Same thing goes with [brother]
The second (\w+) matches the first [brother]
Then $2 matches the [brother] after the first [brother]
1 Reply

Regex Phone Number(nigerian Format) In PythonHelp Me Solve This Java Regex ProblemChallenge: Regex Content Generator234

Learning Python, Using Python At Work/college: Must-try Best Python QuizzesLearn PHP Free On Nairaland, Step By Step GuideHas Anyone Seen This Type Of URL Before