JavaEar 专注于收集分享传播有价值的技术资料

How to include '+' in Regular Expression

I wrote a regular expression (in Objective C) to find the following substrings in a string:

+RA, -RA, RA, +SN, -SN, SN, +DZ, -DZ, DZ

The original RE is:

NSString* expression = @"^.*?(RA|SN|DZ).*?$";

However, since .* implies practically anything, the RE also detects substring "DSNT", for instance, which I am trying to avoid. So instead of ^.*?, I am trying to write something like

@"^(-|+)?(RA|SN|DZ).*?$";

But since the "+" sign has a special meaning in RE, it does not work as intended: the substrings are not identified. So, I wonder how to incorporate the "+" sign in the RE code above.

Many thanks!

3个回答

    最佳答案
  1. The + character has special meaning, either escape it or place these characters inside of a character class:

    [-+]
    

    If you want to match all substrings, remove the anchors from your regular expression and consider this regex:

    NSString* expression = @"([-+]?(?:RA|SN|DZ)\\b)"
    

    Edit

    If this didn't work, you can use the following to match the + character.

    [-\\x2b]
    
  2. 参考答案2
  3. Try following regex,

    .*?(-|\+)?(RA|SN|DZ).*?
    

    enter image description here

  4. 参考答案3
  5. Use this:

    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[+-](?:RA|SN|DZ)\b" options:NSRegularExpressionCaseInsensitive error:&error];
    NSArray *matches = [regex matchesInString:subject options:0 range:NSMakeRange(0, [subject length])];
    NSUInteger matchCount = [matches count];
    if (matchCount) {
        for (NSUInteger matchIdx = 0; matchIdx < matchCount; matchIdx++) {
            NSTextCheckingResult *match = [matches objectAtIndex:matchIdx];
            NSRange matchRange = [match range];
            NSString *result = [subject substringWithRange:matchRange];
        }
    }
    else {  // Nah... No matches.
         }
    

    Explanation

    • The character class [+-] matches a plus or a minus
    • The non-capture group (?:RA|SN|DZ) matches one of the three tokens
    • The code iterates through the matches