Regex Gurus Please Help! - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › Regex Gurus Please Help! (1297 Views)
| 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:Thank you very much. Then what about the brackets (.+?) |
| Re: Regex Gurus Please Help! by Nobody: 4:49am On Nov 22, 2015 |
webninja: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:Ok, thanks man, now how do the $2 come in? Please show some code example ![]() |
| Re: Regex Gurus Please Help! by Nobody: 6:00pm On Nov 22, 2015*. Modified: 5:09am On Nov 23, 2015 |
webninja:$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] |
Regex Phone Number(nigerian Format) In Python • Help Me Solve This Java Regex Problem • Challenge: Regex Content Generator • 2 • 3 • 4
Web Developer/programer Needed For Immediate Employment • You A Programmer? Come In Here • First Bank Launches Chat Banking On Whatsapp
