【问题标题】:regular expression to match " but not \"匹配“但不匹配\”的正则表达式
【发布时间】:2012-11-07 09:21:39
【问题描述】:

如何构造一个匹配文字“的正则表达式,但前提是它前面没有转义斜杠,即 \

我有一个 NSMutableString str,它在 NSLog 上打印以下内容。该字符串是从在线服务器接收的。

"Hi, check out \"this book \". Its cool"

我想对其进行更改,使其在 NSLog 上打印以下内容

Hi, check out "this book ". Its cool

我最初使用的是 replaceOccurencesOfString ""\" 和 ""。但它会执行以下操作:

Hi, check out \this book \. Its cool

所以,我得出结论,我需要上面的正则表达式只匹配 " 但不匹配 \",然后只替换那些双引号。

谢谢 mbh

【问题讨论】:

  • 我很困惑。如果你没有转义斜线,那么 NSString 会抛出一个字符错误并拒绝编译。
  • 字符串的开头和结尾是否真的有"
  • 我将问题更新为更清晰。
  • @mbh,我的回答怎么样,和你想要的一样吗?
  • 如果字符串包含两个反斜杠后跟一个双引号怎么办?示例:\\"。输出应该是什么?

标签: iphone objective-c ios regex


【解决方案1】:
[^\\]\"

[^m] 表示不匹配 m

【讨论】:

    【解决方案2】:

    不确定这会如何转化为 iOS api 中支持的任何内容,但是,如果它们支持锚定(我认为所有正则表达式引擎都应该),那么您正在描述类似

    (^|[^\])"

    即匹配:

    1. 字符串 ^ 的开头或任何不是 \ 后跟:
    2. " 字符

    如果您想进行任何形式的替换,您必须获取正则表达式中的第一个(也是唯一一个)组(即表达式的括号分组部分)并在替换中使用它。通常这个值在你的替换字符串中标记为 $1 或 \1 或类似的东西。

    如果正则表达式引擎基于PCRE,当然您可以将分组表达式放在后面,这样您就不需要捕获并将捕获保存在替换中。

    【讨论】:

      【解决方案3】:

      不确定正则表达式,一个更简单的解决方案是,

      NSString *str = @"\"Hi, check out \\\"this book \\\". Its cool\"";
      NSLog(@"string before modification = %@", str);    
      str = [str stringByReplacingOccurrencesOfString:@"\\\"" withString:@"#$%$#"];
      str = [str stringByReplacingOccurrencesOfString:@"\"" withString:@""];
      str = [str stringByReplacingOccurrencesOfString:@"#$%$#" withString:@"\\\""];//assuming that the chances of having '#$%$#' in your string is zero, or else use more complicated word
      NSLog(@"string after modification = %@", str);
      

      输出:

      string before modification = "Hi, check out \"this book \". Its cool"
      string after modification = Hi, check out \"this book \". Its cool
      

      正则表达式:[^\"].*[^\"]. 给出,Hi, check out \"this book \". Its cool

      【讨论】:

      • 我想到了那个 hack。但正在寻找合适的解决方案。无论如何感谢您的帖子。
      • 我能想到的正则表达式的关闭是这个 [^\"].*[^\"].
      【解决方案4】:

      它看起来像是一个 JSON 字符串?也许在服务器上的 PHP 中使用 json_encode() 创建?您应该在 iOS 中使用正确的 JSON 解析器。不要使用正则表达式,因为您会遇到错误。

      // fetch the data, eg this might return "Hi, check out \"this book \". Its cool"
      NSData *data = [NSData dataWithContentsOfURL:@"http://example.com/foobar/"];
      
      // decode the JSON string
      NSError *error;
      NSString *responseString = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
      
      // check if it worked or not
      if (!responseString || ![responseString isKindOfClass:[NSString class]]) {
         NSLog(@"failed to decode server response. error: %@", error);
         return;
      }
      
      // print it out
      NSLog(@"decoded response: %@", responseString);
      

      输出将是:

      Hi, check out "this book ". Its cool
      

      注意:JSON 解码 API 接受 NSData 对象,而不是 NSString 对象。我假设您也有一个数据对象,并在某个时候将其转换为字符串......但如果您没有,您可以使用以下方法将 NSString 转换为 NSData:

      NSString *responseString = [NSJSONSerialization JSONObjectWithData:[myString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:&error];
      

      有关 JSON 的更多详细信息,请访问:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多