iPhone - URL 有效性的 Reg Exp

发布于 2024-11-09 20:27:00 字数 427 浏览 4 评论 0原文

我有一个聊天视图,用户可以在其中互相发送网址。 如果是 url,我想让用户按下链接并打开网页视图。

我正在使用 IFTweetLabel,它使用 RegexKitLite。 目前唯一可用的支持是 url 以 http/https 开头。 我想支持没有http的链接,例如:www.nytimes.com,甚至没有“www”,nytimes.com。 (以及一堆其他扩展)。

这是 http/s 前缀 reg exp :

@"([hH][tT][tT][pP][sS]?:\\/\\/[^ ,'\">\\]\\)]*[^\\. ,'\">\\]\\)])

有人可以告诉我我需要回答我的其他要求的其他正则表达式吗?

我尝试使用这个,但是将其添加到 Objective C 代码中会产生很多问题。

谢谢

I have a chat view, where users can send urls to one another.
In case of a url, I want to let the user press on the link and open a web view.

I'm using IFTweetLabel which uses RegexKitLite.
Currently the only support available is if the url starts with http/https.
I want to support links without the http, for example : www.nytimes.com , and even without the "www" , nytimes.com. (and bunch of other extentions).

This is the http/s prefix reg exp :

@"([hH][tT][tT][pP][sS]?:\\/\\/[^ ,'\">\\]\\)]*[^\\. ,'\">\\]\\)])

Can someone tell me the other regular expressions I need to answer my other requirements.

I tried using This one, but adding it to objective c code generates a lot of issues.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

謌踐踏愛綪 2024-11-16 20:27:00

以下是 John Grubers URL 匹配正则表达式

(?i)\b(?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’])

以下是我通过混合我拥有的其他一些正则表达式而提出的正则表达式周围和一大块 Grubers 正则表达式:

(?i)\b(?:(?:[a-z][\w\-]+://(?:\S+?(?::\S+?)?\@)?)|(?:(?:[a-z0-9\-]+\.)+[a-z]{2,4}))(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]*\)))*\))*(?<![\s`!()\[\]{};:'".,<>?«»“”‘’])

以下是一个示例程序,通过 RegexKitLite 演示了每个正则表达式与示例文本的匹配内容:

你看到了吗
http://www.stackoverflow.com?或者
http://www.stackoverflow.com/

然后还有
www.stackoverflow.com/,以及
www.stackoverflow.com/index。

也许类似于 stackoverflow.com
有额外的 stackoverflow.com 吗?或者
“stackoverflow.com”?

也许 jobs.stackoverflow.com,或者
'http://twitter.com/#!/CHOCKENBERRY',
木栓!!

文件
@file:///Users/johne/rkl/rkl.html#RegexKitLiteCookbook?

也许
http://www.yahoo.com/index///i.html
http://www.yahoo.com/////xyz.html?!

代码:

#import <Foundation/Foundation.h>
#import "RegexKitLite.h"

int main(int argc, char *argv[]) {
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  NSString *urlRegex = @"(?i)\\b(?:(?:[a-z][\\w\\-]+://(?:\\S+?(?::\\S+?)?\\@)?)|(?:(?:[a-z0-9\\-]+\\.)+[a-z]{2,4}))(?:[^\\s()<>]+|\\((?:[^\\s()<>]+|(?:\\([^\\s()<>]*\\)))*\\))*(?<![\\s`!()\\[\\]{};:'\".,<>?«»“”‘’])";

  // John Gruber's URL matching regex from http://daringfireball.net/2010/07/improved_regex_for_matching_urls
  NSString *gruberURLRegex = @"(?i)\\b(?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’])";

  NSString *urlString = @"Did you see http://www.stackoverflow.com?  Or http://www.stackoverflow.com/?\n\nAnd then there is www.stackoverflow.com/, along with www.stackoverflow.com/index.\n\nMaybe something like stackoverflow.com with extra stackoverflow.com?  Or \"stackoverflow.com\"?\n\nPerhaps jobs.stackoverflow.com, or 'http://twitter.com/#!/CHOCKENBERRY', the CHOCKLOCK!!\n\nFile @file:///Users/johne/rkl/rkl.html#RegexKitLiteCookbook?\n\nMaybe http://www.yahoo.com/index///i.html!  http://www.yahoo.com/////xyz.html?!";

  NSLog(@"String :\n\n%@\n\n", urlString);

  NSLog(@"Matches: %@\n", [urlString componentsMatchedByRegex:urlRegex]);

  NSLog(@"Gruber URL Regex Matches: %@\n", [urlString componentsMatchedByRegex:gruberURLRegex]);

  [pool release]; pool = NULL;
  return(0);
}

编译为:

shell% gcc -o url url.m RegexKitLite.m -framework Foundation -licucore

运行时:

shell% ./url
2011-05-27 20:32:58.204 url[25520:903] String :

Did you see http://www.stackoverflow.com?  Or http://www.stackoverflow.com/?

And then there is www.stackoverflow.com/, along with www.stackoverflow.com/index.

Maybe something like stackoverflow.com with extra stackoverflow.com?  Or "stackoverflow.com"?

Perhaps jobs.stackoverflow.com, or 'http://twitter.com/#!/CHOCKENBERRY', the CHOCKLOCK!!

File @file:///Users/johne/rkl/rkl.html#RegexKitLiteCookbook?

Maybe http://www.yahoo.com/index///i.html!  http://www.yahoo.com/////xyz.html?!

2011-05-27 20:32:58.211 url[25520:903] Matches: (
    "http://www.stackoverflow.com",
    "http://www.stackoverflow.com/",
    "www.stackoverflow.com/",
    "www.stackoverflow.com/index",
    "stackoverflow.com",
    "stackoverflow.com",
    "stackoverflow.com",
    "jobs.stackoverflow.com",
    "http://twitter.com/#!/CHOCKENBERRY",
    "file:///Users/johne/rkl/rkl.html#RegexKitLiteCookbook",
    "http://www.yahoo.com/index///i.html",
    "http://www.yahoo.com/////xyz.html"
)
2011-05-27 20:32:58.213 url[25520:903] Gruber URL Regex Matches: (
    "http://www.stackoverflow.com",
    "http://www.stackoverflow.com/",
    "www.stackoverflow.com/",
    "www.stackoverflow.com/index",
    "http://twitter.com/#!/CHOCKENBERRY",
    "file:///Users/johne/rkl/rkl.html#RegexKitLiteCookbook",
    "http://www.yahoo.com/index///i.html",
    "http://www.yahoo.com/////xyz.html"
)

编辑 2011/05/27: 对正则表达式进行了细微更改,以修复与 ( ) 括号正确。

编辑2011/05/27:发现上面的正则表达式不能很好地处理一些额外的极端情况。更新了正则表达式:

(?i)\b(?:[a-z][\w\-]+://(?:\S+?(?::\S+?)?\@)?)?(?:(?:(?<!:/|\.)(?:(?:[a-z0-9\-]+\.)+[a-z]{2,4}(?![a-z]))|(?<=://)/))(?:(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]*\)))*\))*)(?<![\s`!()\[\]{};:'".,<>?«»“”‘’])

...作为 Obj-C 字符串:

@"(?i)\\b(?:[a-z][\\w\\-]+://(?:\\S+?(?::\\S+?)?\\@)?)?(?:(?:(?<!:/|\\.)(?:(?:[a-z0-9\\-]+\\.)+[a-z]{2,4}(?![a-z]))|(?<=://)/))(?:(?:[^\\s()<>]+|\\((?:[^\\s()<>]+|(?:\\([^\\s()<>]*\\)))*\\))*)(?<![\\s`!()\\[\\]{};:'\".,<>?«»“”‘’])";

OP 还询问如何确保尾随 TLD 是“有效”。这是相同的正则表达式,采用 Obj-C 字符串形式,包含所有当前有效的 TLD(截至2011年5月27日):

@"(?i)\\b(?:[a-z][\\w\\-]+://(?:\\S+?(?::\\S+?)?\\@)?)?(?:(?:(?<!:/|\\.)(?:(?:[a-z0-9\\-]+\\.)+(?:(ac|ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|asia|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cat|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|info|int|io|iq|ir|is|it|je|jm|jo|jobs|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mobi|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|travel|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|xn--0zwm56d|xn--11b5bs3a9aj6g|xn--3e0b707e|xn--45brj9c|xn--80akhbyknj4f|xn--90a3ac|xn--9t4b11yi5a|xn--clchc0ea0b2g2a9gcd|xn--deba0ad|xn--fiqs8s|xn--fiqz9s|xn--fpcrj9c3d|xn--fzc2c9e2c|xn--g6w251d|xn--gecrj9c|xn--h2brj9c|xn--hgbk6aj7f53bba|xn--hlcj6aya9esc7a|xn--j6w193g|xn--jxalpdlp|xn--kgbechtv|xn--kprw13d|xn--kpry57d|xn--lgbbat1ad8j|xn--mgbaam7a8h|xn--mgbayh7gpa|xn--mgbbh1a71e|xn--mgbc0a9azcg|xn--mgberp4a5d4ar|xn--o3cw4h|xn--ogbpf8fl|xn--p1ai|xn--pgbs0dh|xn--s9brj9c|xn--wgbh1c|xn--wgbl6a|xn--xkc2al3hye2a|xn--xkc2dl3a5ee0h|xn--yfro4i67o|xn--ygbi2ammx|xn--zckzah|xxx|ye|yt|za|zm|zw))(?![a-z]))|(?<=://)/))(?:(?:[^\\s()<>]+|\\((?:[^\\s()<>]+|(?:\\([^\\s()<>]*\\)))*\\))*)(?<![\\s`!()\\[\\]{};:'\".,<>?«»“”‘’])";

The following is John Grubers URL Matching Regex:

(?i)\b(?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’])

The following is a regex I came up with by blending a few other regexes I had around and a good chunk of Grubers regex:

(?i)\b(?:(?:[a-z][\w\-]+://(?:\S+?(?::\S+?)?\@)?)|(?:(?:[a-z0-9\-]+\.)+[a-z]{2,4}))(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]*\)))*\))*(?<![\s`!()\[\]{};:'".,<>?«»“”‘’])

The following is a sample program that demonstrates, via RegexKitLite, what each regex matches against the sample text of:

Did you see
http://www.stackoverflow.com? Or
http://www.stackoverflow.com/?

And then there is
www.stackoverflow.com/, along with
www.stackoverflow.com/index.

Maybe something like stackoverflow.com
with extra stackoverflow.com? Or
"stackoverflow.com"?

Perhaps jobs.stackoverflow.com, or
'http://twitter.com/#!/CHOCKENBERRY',
the CHOCKLOCK!!

File
@file:///Users/johne/rkl/rkl.html#RegexKitLiteCookbook?

Maybe
http://www.yahoo.com/index///i.html!
http://www.yahoo.com/////xyz.html?!

The code:

#import <Foundation/Foundation.h>
#import "RegexKitLite.h"

int main(int argc, char *argv[]) {
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  NSString *urlRegex = @"(?i)\\b(?:(?:[a-z][\\w\\-]+://(?:\\S+?(?::\\S+?)?\\@)?)|(?:(?:[a-z0-9\\-]+\\.)+[a-z]{2,4}))(?:[^\\s()<>]+|\\((?:[^\\s()<>]+|(?:\\([^\\s()<>]*\\)))*\\))*(?<![\\s`!()\\[\\]{};:'\".,<>?«»“”‘’])";

  // John Gruber's URL matching regex from http://daringfireball.net/2010/07/improved_regex_for_matching_urls
  NSString *gruberURLRegex = @"(?i)\\b(?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’])";

  NSString *urlString = @"Did you see http://www.stackoverflow.com?  Or http://www.stackoverflow.com/?\n\nAnd then there is www.stackoverflow.com/, along with www.stackoverflow.com/index.\n\nMaybe something like stackoverflow.com with extra stackoverflow.com?  Or \"stackoverflow.com\"?\n\nPerhaps jobs.stackoverflow.com, or 'http://twitter.com/#!/CHOCKENBERRY', the CHOCKLOCK!!\n\nFile @file:///Users/johne/rkl/rkl.html#RegexKitLiteCookbook?\n\nMaybe http://www.yahoo.com/index///i.html!  http://www.yahoo.com/////xyz.html?!";

  NSLog(@"String :\n\n%@\n\n", urlString);

  NSLog(@"Matches: %@\n", [urlString componentsMatchedByRegex:urlRegex]);

  NSLog(@"Gruber URL Regex Matches: %@\n", [urlString componentsMatchedByRegex:gruberURLRegex]);

  [pool release]; pool = NULL;
  return(0);
}

Compile with:

shell% gcc -o url url.m RegexKitLite.m -framework Foundation -licucore

When run:

shell% ./url
2011-05-27 20:32:58.204 url[25520:903] String :

Did you see http://www.stackoverflow.com?  Or http://www.stackoverflow.com/?

And then there is www.stackoverflow.com/, along with www.stackoverflow.com/index.

Maybe something like stackoverflow.com with extra stackoverflow.com?  Or "stackoverflow.com"?

Perhaps jobs.stackoverflow.com, or 'http://twitter.com/#!/CHOCKENBERRY', the CHOCKLOCK!!

File @file:///Users/johne/rkl/rkl.html#RegexKitLiteCookbook?

Maybe http://www.yahoo.com/index///i.html!  http://www.yahoo.com/////xyz.html?!

2011-05-27 20:32:58.211 url[25520:903] Matches: (
    "http://www.stackoverflow.com",
    "http://www.stackoverflow.com/",
    "www.stackoverflow.com/",
    "www.stackoverflow.com/index",
    "stackoverflow.com",
    "stackoverflow.com",
    "stackoverflow.com",
    "jobs.stackoverflow.com",
    "http://twitter.com/#!/CHOCKENBERRY",
    "file:///Users/johne/rkl/rkl.html#RegexKitLiteCookbook",
    "http://www.yahoo.com/index///i.html",
    "http://www.yahoo.com/////xyz.html"
)
2011-05-27 20:32:58.213 url[25520:903] Gruber URL Regex Matches: (
    "http://www.stackoverflow.com",
    "http://www.stackoverflow.com/",
    "www.stackoverflow.com/",
    "www.stackoverflow.com/index",
    "http://twitter.com/#!/CHOCKENBERRY",
    "file:///Users/johne/rkl/rkl.html#RegexKitLiteCookbook",
    "http://www.yahoo.com/index///i.html",
    "http://www.yahoo.com/////xyz.html"
)

EDIT 2011/05/27: Made a minor change to the regex to fix a problem where it wasn't matching ( ) parenthesis correctly.

EDIT 2011/05/27: Found some additional corner cases that the regex above didn't handle well. Updated regex:

(?i)\b(?:[a-z][\w\-]+://(?:\S+?(?::\S+?)?\@)?)?(?:(?:(?<!:/|\.)(?:(?:[a-z0-9\-]+\.)+[a-z]{2,4}(?![a-z]))|(?<=://)/))(?:(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]*\)))*\))*)(?<![\s`!()\[\]{};:'".,<>?«»“”‘’])

... as an Obj-C string:

@"(?i)\\b(?:[a-z][\\w\\-]+://(?:\\S+?(?::\\S+?)?\\@)?)?(?:(?:(?<!:/|\\.)(?:(?:[a-z0-9\\-]+\\.)+[a-z]{2,4}(?![a-z]))|(?<=://)/))(?:(?:[^\\s()<>]+|\\((?:[^\\s()<>]+|(?:\\([^\\s()<>]*\\)))*\\))*)(?<![\\s`!()\\[\\]{};:'\".,<>?«»“”‘’])";

The OP also asked for how to make sure the trailing TLD was "valid". Here's the same regex, in Obj-C string form, with all the the currently valid TLDs (as of 2011/05/27):

@"(?i)\\b(?:[a-z][\\w\\-]+://(?:\\S+?(?::\\S+?)?\\@)?)?(?:(?:(?<!:/|\\.)(?:(?:[a-z0-9\\-]+\\.)+(?:(ac|ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|asia|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cat|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|info|int|io|iq|ir|is|it|je|jm|jo|jobs|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mobi|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|travel|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|xn--0zwm56d|xn--11b5bs3a9aj6g|xn--3e0b707e|xn--45brj9c|xn--80akhbyknj4f|xn--90a3ac|xn--9t4b11yi5a|xn--clchc0ea0b2g2a9gcd|xn--deba0ad|xn--fiqs8s|xn--fiqz9s|xn--fpcrj9c3d|xn--fzc2c9e2c|xn--g6w251d|xn--gecrj9c|xn--h2brj9c|xn--hgbk6aj7f53bba|xn--hlcj6aya9esc7a|xn--j6w193g|xn--jxalpdlp|xn--kgbechtv|xn--kprw13d|xn--kpry57d|xn--lgbbat1ad8j|xn--mgbaam7a8h|xn--mgbayh7gpa|xn--mgbbh1a71e|xn--mgbc0a9azcg|xn--mgberp4a5d4ar|xn--o3cw4h|xn--ogbpf8fl|xn--p1ai|xn--pgbs0dh|xn--s9brj9c|xn--wgbh1c|xn--wgbl6a|xn--xkc2al3hye2a|xn--xkc2dl3a5ee0h|xn--yfro4i67o|xn--ygbi2ammx|xn--zckzah|xxx|ye|yt|za|zm|zw))(?![a-z]))|(?<=://)/))(?:(?:[^\\s()<>]+|\\((?:[^\\s()<>]+|(?:\\([^\\s()<>]*\\)))*\\))*)(?<![\\s`!()\\[\\]{};:'\".,<>?«»“”‘’])";
凤舞天涯 2024-11-16 20:27:00

这将匹配 http://example.org 和 www.example.org。

@"(([hH][tT][tT][pP][sS]?:\\/\\/|www\\.)[^ ,'\">\\]\\)]*\\.[^\\. ,'\">\\]\\)]{2,6})

尽管我添加了“匹配组”,但请检查 RegExp 返回的匹配/搜索结果,以便将正确的参数重新插入到正确的位置。

如果您可以发布整个代码片段,那就更容易了。

正则表达式解释:

(
    (
        [hH][tT][tT][pP][sS]?:\/\/    # Match HTTP/http (and hTtP :)
        |                             # OR
        www\.                         # www<literal DOT>
    )
    [^ ,'\">\]\)]*                    # Match at least 1 character that are not any of space, comma, apostrophe, quotation mark, "more than", "right square bracket", "right parenthese"
    \.                                # Match <literal DOT>
    [^\. ,'\">\]\)]{2,6}              # Match 2-6 characters that are not any of dot, space, comma, apostrophe, quotation mark, "more than", "right square bracket", "right parenthese"
)

This will match both http://example.org and www.example.org.

@"(([hH][tT][tT][pP][sS]?:\\/\\/|www\\.)[^ ,'\">\\]\\)]*\\.[^\\. ,'\">\\]\\)]{2,6})

Although i added a "match group", so check the match/search result returned by the RegExp so the right parameters are re-inserted in the right place.

If you could post the entire code snippet, it would be easier.

RegExp explanation:

(
    (
        [hH][tT][tT][pP][sS]?:\/\/    # Match HTTP/http (and hTtP :)
        |                             # OR
        www\.                         # www<literal DOT>
    )
    [^ ,'\">\]\)]*                    # Match at least 1 character that are not any of space, comma, apostrophe, quotation mark, "more than", "right square bracket", "right parenthese"
    \.                                # Match <literal DOT>
    [^\. ,'\">\]\)]{2,6}              # Match 2-6 characters that are not any of dot, space, comma, apostrophe, quotation mark, "more than", "right square bracket", "right parenthese"
)
缘字诀 2024-11-16 20:27:00

您不想为此使用正则表达式。

您需要一个 NSDataDetector,它会为您找到所有内容。

You don't want to use a regular expression for this.

You want an NSDataDetector, and it'll find them all for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文