2012-01-21 16 views
0

の一部を選択:PHPでNSRegularExpression、このような文字列が与えられた文字列

Seat 2: Pet21 ($1.98 in chips) 

あなたはこのように、あなたの正規表現で括弧を使用して正規表現の一部を選択することができます。

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

このシート番号、ユーザーID、チップ量を選択します。

Array 
(
    [0] => Array 
     (
      [0] => Seat 1: [email protected] ($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] => [email protected] 
      [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 
     ) 

) 

目的-c wiで同様のことを行う方法はありますか第NSRegularExpression

編集:あなたはおそらくNSTextCheckingResultを返しNSRegularExpressionfirstMatchInString:options:range:方法を、使用したい

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

    } 

答えて

3

:これまでのところ私はこれを持っています。次に、サブタイプの範囲を取得するにはのrangeAtIndex:メソッドを使用します。サブクラスのテキストを取得するには、元NSStringsubstringWithRange:メソッドに範囲を渡すことができます。

+0

それは、ありがとう! –

+0

良い答え、小さな追加:リテラル文字列でバックスラッシュをエスケープする必要があります。つまり、 '\ .'は' \\。 'でなければなりません。 – omz

関連する問題