简体   繁体   English

使用正则表达式从字符串中排除特定字符

[英]Exclude specific characters from a string using regex

I have a regex that extracts typed URL's from a string (a description text that could include typed URL's) and converts them to href's. 我有一个正则表达式,从字符串中提取键入的URL(一个描述文本,可能包括键入的URL)并将它们转换为href的。 This all works fine except for the fact that when a URL is typed and it's last character is a "," of ")" it also takes this character as part of the URL. 这一切都很好,除了这样一个事实,即当键入一个URL并且它的最后一个字符是“,”“)”时,它也将此字符作为URL的一部分。 How could I prevent this? 我怎么能阻止这个?

Example text: 示例文字:

Hi this is my beautiful message which contains a link (see www.website.com) and some more info. 嗨,这是我的精彩信息,其中包含一个链接(请参阅www.website.com)以及更多信息。

My regex reads the URL but also takes the last character ")" when it creates the href, resulting in a bad link. 我的正则表达式读取URL但在创建href时也使用最后一个字符“)”,从而导致链接错误。

My Regex: 我的正则表达式:

preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $text);

Include "," and ")" in the list of URL terminating characters. 在URL终止字符列表中包含“,”和“)”。 Don't forget to escape ")" with a backslash. 别忘了用反斜杠逃避“)”。 In other words, try: 换句话说,尝试:

preg_replace("#(^|[\\n ])((www|ftp)\\.[^ \\"\\t\\n\\r< \\),]*)#", "\\\\1<a href=\\"http://\\\\2\\" target=\\"_blank\\">\\\\2</a>", $text);

(I haven't tested this.) (我没有测试过这个。)

看起来你在url.try之后匹配任何东西。

preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ][^\,)]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $text);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM