E1M1 Posted January 13, 2012 Share Posted January 13, 2012 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 Link to comment Share on other sites More sharing options...
JohnQSmith Posted January 13, 2012 Share Posted January 13, 2012 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". Link to comment Share on other sites More sharing options...
E1M1 Posted January 13, 2012 Author Share Posted January 13, 2012 Thank you very much. edited Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now