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
        )

)

NSRegularExpressionを使用してobjective-cで同様のことを行う方法はありますか

編集:これまでのところ、私はこれを持っています:

    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)]) 
    {

    }
4

1 に答える 1

3

おそらく、 を返すfirstMatchInString:options:range:メソッド ofを使用したいと思うでしょう。次に、 のメソッドを使用して、各サブマッチの範囲を取得します。元のメソッドに範囲を渡して、サブマッチのテキストを取得できます。NSRegularExpressionNSTextCheckingResultrangeAtIndex:NSTextCheckingResultsubstringWithRange:NSString

于 2012-01-21T08:04:10.850 に答える