2012-01-21 5 views
0

이 같은 문자열을 감안할 때 :NSRegularExpression, 문자열의 선택 부분은

는 PHP에서
Seat 2: Pet21 ($1.98 in chips) 

당신이 당신의 정규식에서 괄호를 사용하여 정규 표현식의 일부를 선택할 수 있습니다, 다음과 같이 :

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

이를 목표 -C (WI)에서 유사 할 수있는 방법이 있는가

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 
     ) 

) 

: 좌석 번호, 사용자 ID와 칩의 크기를 선택 일 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)]) 
    { 

    } 

답변

3

당신은 아마 NSTextCheckingResult 반환 NSRegularExpressionfirstMatchInString:options:range: 방법을 사용하고 싶습니다. 그런 다음 rangeAtIndex: 메소드 NSTextCheckingResult을 사용하여 각 서브맵의 범위를 가져 오려고합니다. 하위 substringWithRange: 방법의 범위를 원래 NSString으로 전달하여 하위 텍스트를 가져올 수 있습니다.

+0

그건 그랬어, 많이 고마워! –

+0

좋은 답변, 작은 추가 : 리터럴 문자열에서 백 슬래시를 이스케이프해야합니다. 즉, '\ .'은'\\. '이어야합니다. – omz