简体   繁体   English

将NSRegularExpression更新为特定模式

[英]Updating an NSRegularExpression to be a specific pattern

I have a NSString pattern like so: 我有一个像这样的NSString模式:

NSString *pattern = @"@[A-Za-z0-9]+";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];

This pattern shows all the matches that start with @ and have at least one alphanumeric character after it. 此模式显示所有以@开头且后接至少一个字母数字字符的匹配项。

How do I adopt this so that the pattern matches all alphanumeric characters, _ or - and start and end with an alphanumeric character? 如何采用这种方式,以便模式匹配所有字母数字字符_或-并以字母数字字符开头和结尾?

Some examples are: 一些例子是:

@a
@0
@a-z
@hello
@ab_z9

Some edge cases are: 一些边缘情况是:

If it is @Liam_O'Flaherty then I want it to match to @Liam_O
Or
If it is @a- then I want it to match to @a

Try this regex: 试试这个正则表达式:

@"@[a-zA-Z0-9](?:(?:[A-Za-z0-9-_]*[a-zA-Z0-9])|)"

The first bracket groups the alphanumeric character, the second matches alphanumeric and the - and _ , and the last matches alphanumeric at the end of the word. 第一个括号将字母数字字符分组,第二个括号将字母数字字符与-_匹配,最后一个括号将单词末尾的字母数字匹配。 The * means that we can have any or none of the second bracket group, the (?:) parentheses create situations for Regex to match but not create backreferences/match groups, and the | *表示我们可以有第二个括号组中的任何一个,也可以不包含,括号中的(?:)会使Regex匹配,但不能创建反向引用/匹配组,而| means OR... So we can have an alphanumeric character, and then either some 0+ number of alphanumerics, - , and _ , followed by another alphanumeric, or nothing. 意味着OR ...所以我们可以有一个字母数字字符,然后是0+数量的字母数字-_ ,后跟另一个字母数字,否则什么也没有。 (as nothing follows the or) (因为没有跟随或)

PS Not quite sure in your question if you need the opening @ or not. PS不太确定您的问题是否需要@ If not, take it out... 如果没有,将其取出...

I would consider something like the following: 我会考虑以下内容:

@(?=[A-Za-z0-9])[A-Za-z0-9-_]+(?<=[A-Za-z0-9])

The constituent parts of this are: 其组成部分是:

  • The @ followed later by the [A-Za-z0-9-_]+ is the heart of the search, matching any string with 1 or more alpha numeric characters, hyphens or underscores. @后面是[A-Za-z0-9-_]+是搜索的心脏,它匹配具有1个或多个字母数字字符,连字符或下划线的任何字符串。

  • The look-ahead assertion at the start, (?=[A-Za-z0-9]) , means "but it must start with alphanumeric." 开头的前瞻性断言(?=[A-Za-z0-9])表示“但必须以字母数字开头”。

  • The look-behind assertion at the end, (?<=[A-Za-z0-9]) , means "and it must end with alpha numeric." 结尾处的后向断言(?<=[A-Za-z0-9])表示“并且必须以字母数字结尾”。

This raises a few edge-case questions, namely: 这就提出了一些极端的问题,即:

  • What do you want to do with accents? 您要如何处理口音? If you wanted to handle accented characters, such as @naïve or @resumé , you might want to use \\p{L} rather than A-Za-z . 如果要处理带重音符号的字符(例如@naïve@resumé ,则可能要使用\\p{L}而不是A-Za-z (And if you put this in a string in your code, you need to escape the backslash, so that would be represented with \\\\p{L} .) (并且,如果将其放在代码中的字符串中,则需要转义反斜杠,以便用\\\\p{L} 。)

  • What do you want to do if there is an non-alphanumeric character in the string, for example @this.is.wrong or @Liam_O'Flaherety . 做你想做的事,如果有是字符串中的非字母数字字符,例如什么@this.is.wrong@Liam_O'Flaherety Or what do you want to do if it does not end in alpha numeric, eg @a- . 或者,你想,如果它以字母数字,例如到底该干什么做@a- The above regex (as well as the regex presented in other answers) would match up to the invalid character (eg @this , @Liam_O , and @a , respectively). 上述正则表达式(以及在其他的答案给出的正则表达式)将匹配于无效字符(例如@this@Liam_O ,和@a ,分别地)。 This doesn't seem like it could possibly be the right handling of this scenario. 看来这可能不是对这种情况的正确处理。 Personally, I would be inclined to further qualify the regex to exclude these cases, but without a broader description of your business problem, it's hard to say what's right in this case. 就我个人而言,我倾向于进一步限定正则表达式以排除这些情况,但是如果不对您的业务问题进行更广泛的描述,则很难说出在这种情况下正确的方法。

    Having said that, I would wager you might not concerned with this exception, so this flaw in the regex might not be of concern to you. 话虽如此,我打赌您可能不关心此异常,因此正则表达式中的此缺陷可能与您无关。 But if you are, let us know what the edge-cases are and how you'd like to handle them and we can be more specific in our answers. 但是,如果您愿意,请告诉我们什么是极端情况,以及您希望如何处理这些极端情况,我们可以在答复中更加具体。

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

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