NSRegularExpression,选择字符串的一部分

发布于 2024-12-27 23:11:57 字数 1808 浏览 1 评论 0原文

给定这样的字符串:

Seat 2: Pet21 ($1.98 in chips) 

在 php 中,您可以使用正则表达式中的括号选择正则表达式的一部分,如下所示:

Seat ([0-9]+): (.{1,12}) .[$|€|£]([0-9]+\.?[0-9]{0,2}) in chips.

这选择座位号、用户 ID 和筹码数量:

Array
(
    [0] => Array
        (
            [0] => Seat 1: S@pphiR ($0.83 in chips)
            [1] => Seat 2: Pet21 ($1.98 in chips)
            [2] => Seat 3: derphurp ($2.76 in chips)
            [3] => Seat 4: -M-A-R-K-qaz ($0.92 in chips)
            [4] => Seat 5: Rolle55 ($2.45 in chips)
            [5] => Seat 6: SanderDecler ($2 in chips)
        )

    [1] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
        )

    [2] => Array
        (
            [0] => S@pphiR
            [1] => Pet21
            [2] => derphurp
            [3] => -M-A-R-K-qaz
            [4] => Rolle55
            [5] => SanderDecler
        )

    [3] => Array
        (
            [0] => 0.83
            [1] => 1.98
            [2] => 2.76
            [3] => 0.92
            [4] => 2.45
            [5] => 2
        )

)

有没有办法在 Objective- 中执行类似操作c 与 NSRegularExpression

编辑:到目前为止我已经得到了这个:

    NSRegularExpression *regex = 
    [NSRegularExpression regularExpressionWithPattern:@"Seat [0-9]+: (.{1,12}) .[$|€|£][0-9]+\.?[0-9]{0,2} in chips."
                                              options:0 
                                                error:nil];

    for (NSTextCheckingResult *result in [regex matchesInString:history options:NSMatchingReportCompletion range:NSMakeRange(0, history.length)]) 
    {

    }

Given a string like this:

Seat 2: Pet21 ($1.98 in chips) 

In php you can select a part of a regex using parentheses in your regex, like this:

Seat ([0-9]+): (.{1,12}) .[$|€|£]([0-9]+\.?[0-9]{0,2}) in chips.

This selects the seat number, user id and the amount of chips:

Array
(
    [0] => Array
        (
            [0] => Seat 1: S@pphiR ($0.83 in chips)
            [1] => Seat 2: Pet21 ($1.98 in chips)
            [2] => Seat 3: derphurp ($2.76 in chips)
            [3] => Seat 4: -M-A-R-K-qaz ($0.92 in chips)
            [4] => Seat 5: Rolle55 ($2.45 in chips)
            [5] => Seat 6: SanderDecler ($2 in chips)
        )

    [1] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
        )

    [2] => Array
        (
            [0] => S@pphiR
            [1] => Pet21
            [2] => derphurp
            [3] => -M-A-R-K-qaz
            [4] => Rolle55
            [5] => SanderDecler
        )

    [3] => Array
        (
            [0] => 0.83
            [1] => 1.98
            [2] => 2.76
            [3] => 0.92
            [4] => 2.45
            [5] => 2
        )

)

Is there a way to do the similar in objective-c with NSRegularExpression

edit: So far I've got this:

    NSRegularExpression *regex = 
    [NSRegularExpression regularExpressionWithPattern:@"Seat [0-9]+: (.{1,12}) .[$|€|£][0-9]+\.?[0-9]{0,2} in chips."
                                              options:0 
                                                error:nil];

    for (NSTextCheckingResult *result in [regex matchesInString:history options:NSMatchingReportCompletion range:NSMakeRange(0, history.length)]) 
    {

    }

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

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

发布评论

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

评论(1

夜灵血窟げ 2025-01-03 23:11:57

您可能想要使用 NSRegularExpressionfirstMatchInString:options:range: 方法,该方法返回一个 NSTextCheckingResult。然后你想使用NSTextCheckingResultrangeAtIndex:方法来获取每个子匹配的范围。您可以将范围传递给原始 NSStringsubstringWithRange: 方法来获取子匹配的文本。

You probably want to use the firstMatchInString:options:range: method of NSRegularExpression, which returns an NSTextCheckingResult. Then you want to use the rangeAtIndex: method of NSTextCheckingResult to get the range of each submatch. You can pass a range to the substringWithRange: method of the original NSString to get the text of a submatch.

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