Jump to content

Recommended Posts

Posted

Does anyone know if it's possible to write regex pattern where so that \2 would be "I am url" ? Currently it's \4. If I replace ((http|https|news|ftp)://(.*?)) with (.*?) then it works as I want (\2 is "I am url"). I would like to know if it's possible to write such pattern which would still require http|https|news|ftp at the beginning but which would give "I am url" as 2nd match instead of 4th

$SEARCH =  '\[url\="((http|https|news|ftp)://(.*?))"\](.*?)\[\/url\]'
$REPLACE = '<a href="\1">\2</a>'
$STR = '[url="http://google.com"]I am url[/url]'
ConsoleWrite(StringRegExpReplace($STR,$SEARCH,$REPLACE)&@CRLF)

edited

Posted

Does anyone know if it's possible to write regex pattern where so that 2 would be "I am url" ? Currently it's 4. If I replace ((http|https|news|ftp)://(.*?)) with (.*?) then it works as I want (2 is "I am url"). I would like to know if it's possible to write such pattern which would still require http|https|news|ftp at the beginning but which would give "I am url" as 2nd match instead of 4th

You need to add " ?: " to the second and third matching group (parentheses) so that they become non-matching groups. This turns the fourth group into the second group and your regex works as you'd like.

$SEARCH =  '[url="((?:http|https|news|ftp)://(?:.*?))"](.*?)[/url]'
$REPLACE = '<a href="1">2</a>'
$STR = '[url="http://google.com"]I am url[/url]'
ConsoleWrite(StringRegExpReplace($STR,$SEARCH,$REPLACE)&@CRLF)

Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...