Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,445 members, 7,808,592 topics. Date: Thursday, 25 April 2024 at 01:57 PM

Regex Gurus Please Help! - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Regex Gurus Please Help! (1161 Views)

Regex Phone Number(nigerian Format) In Python / Help Me Solve This Java Regex Problem / Challenge: Regex Content Generator (2) (3) (4)

(1) (Reply) (Go Down)

Regex Gurus Please Help! by webninja: 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

2 Likes

Re: Regex Gurus Please Help! by webninja: 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: 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 Jelo4kul(m): 6:00pm On Nov 22, 2015
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]

2 Likes

(1) (Reply)

Rastvorlabs Code Week, Ota. / Urgently Needed / I Need A Programmer Who Reside In Lagos

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 13
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.