2012-05-13 5 views
0

Excluding strings using regex과 거의 동일하게하고 싶지만 Regex를 사용하여 iOS를하고 싶습니다. 그래서 기본적으로 내가 문자열에서 일치하는 항목을 찾은 다음 나는이 같은 문자열을 가지고, 그래서 만약 문자열에서 제거 할, 내가 원하는 Hello #world @something#world & @something을 찾아 다음 그냥 Hello가되도록 문자열에서 제거합니다. 나는 이미 내가 Hello #world으로 결국 첫 번째와 두 번째 Hello @something에 대한 그래서이iOS에서 Regex를 사용하여 한 문자열에서 문자열 제외

NSString *stringWithoutAt = [input stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"@%@",atString] withString:@""]; 
NSString *stringWithoutTag = [input stringByReplacingOccurrencesOfString:tagString withString:@""]; 

를 수행하여 @ 문제를 해결 #worldsomething 아니라 @, #[\\p{Letter}]+|[^@]+$을 제거하는이 표현이있다. 하지만 Regex 또는 다른 것을 사용하여 #world@something을 동시에 제거하는 방법이 있습니까?

답변

2

두 가지 방법으로 아이폰에서 정규 표현식을 사용할 수 있습니다 : - NSTextCheckingResult

NSRegularExpression & 사용> 프레임 워크 see the tutorial

이 여기에

NSStirng *[email protected]"Your String"; 
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"@[a-z]*#[a-z]*" options:NSRegularExpressionCaseInsensitive error:&error]; 
[regex enumerateMatchesInString:string options:0 range:NSMakeRange(0, [string length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) 
{ 
    // your statement if it matches 
}]; 

어떤 표현을 RegExKitLIte를 사용

1> @ 표현 후 #을 연결 한 후 #

및 명세서에서 i를 공간에 의해 t는 표현

얻을 수 u는 단순히 변형 된 문자열이 작업을 수행하려면 : -

NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 
range:NSMakeRange(0, [string length]) withTemplate:@"$2$1"]; 
+0

미안하지만, 당신이 무엇을하고 있는지 정확히 설명 할 수 있습니까? – Souljacker

+0

정규식을 사용하는 것과는 별개의 방법이 있습니까? – Souljacker

+0

표현식 @ [letters] # [letters]와 일치하는 정규 표현식을 작성한 다음 제공된 블록에서이를 @ ""대체 할 수 있습니다. –

관련 문제