除非在引号内,否则csharp删除空格,忽略转义引号

时间:2016-01-13 15:16:39

标签: c# regex escaping quotes spaces

我正在使用csharp,并且有一个json字符串,我想删除所有不在引号内的空格。我在线搜索,我已经找到了一个解决方案,其中包括:

import Foundation
import UIKit
import AVFoundation


class VC11 : UIViewController {


    @IBOutlet weak var timerLabel: UILabel!



    var timer = NSTimer()
    var count = 240
    var timerRunning = false
    var audioPlayer = AVAudioPlayer()


    override func viewDidLoad() {
        super.viewDidLoad()


            func nextPage(sender:UISwipeGestureRecognizer) {
                switch sender.direction {

                case UISwipeGestureRecognizerDirection.Left:
                    print("SWIPED LEFT")
                    self.performSegueWithIdentifier("seg11", sender: nil)
                default:
                    break

                }


                var leftSwipe = UISwipeGestureRecognizer (target: self, action: Selector("nextPage"))
                var rightSwipe = UISwipeGestureRecognizer (target: self, action: Selector("nextPage"))

                leftSwipe.direction = .Left
                rightSwipe.direction = .Right

                view.addGestureRecognizer(leftSwipe)
                view.addGestureRecognizer(rightSwipe)
            }


        }



    func updateTime() {
        count--

            let seconds = count % 60
            let minutes = (count / 60) % 60
            let hours = count / 3600
            let strHours = hours > 9 ? String(hours) : "0" + String(hours)
            let strMinutes = minutes > 9 ? String(minutes) : "0" + String(minutes)
            let strSeconds = seconds > 9 ? String(seconds) : "0" + String(seconds)
            if hours > 0 {
                timerLabel.text = "\(strHours):\(strMinutes):\(strSeconds)"
            }
            else {
                timerLabel.text = "\(strMinutes):\(strSeconds)"

        }
        stopTimer()


           }
  @IBAction func startTimer(sender: AnyObject) {
    timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)

    sender.setTitle("Running...", forState: .Normal)

       }


    func stopTimer()

    {

        if count == 0 {
            timer.invalidate()
            timerRunning = false
            timerLabel.text = "04:00"
            playSound()
            count = 240

        }


    }




    func playSound() {

    var soundPath = NSBundle.mainBundle().pathForResource("Metal_Gong", ofType: "wav")
    var soundURL = NSURL.fileURLWithPath(soundPath!)


        self.audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: nil)
        self.audioPlayer.play()
    }


}

但是,我现在正处理一个包含转义引号的字符串:

aidstring = Regex.Replace(aidstring, "\\s+(?=([^\"]*\"[^\"]*\")*[^\"]*$)", "");

以上的正则表达式解决方案将其转换为:

"boolean": "k near/3 \"funds private\""

因为转义引号被视为正常引号。 任何人都可以发布一个正则表达式,其中被忽略的转义引号?

非常感谢!

乔瓦尼

2 个答案:

答案 0 :(得分:1)

只是一个想法......而且这并不会立即看起来合法,因为存在明显的可能缺陷。但如果你考虑一下,那些失败的场景几乎没有发生的可能性:

Regex.Replace(aidstring, @"\"\s*:\s*\"", "\":\"");

长话短说,寻找你想要替换的空间,而不是寻找你不想要替换的所有空间:

"boolean"    :    "k near/3 \"funds private\""
         ^^^^^^^^^

它失败的唯一时间是json对象的实际值 - 内容实际上是冒号...让我知道这种情况经常发生的时间。 :)

但是Skeet是最正确的。使用Json Parser进行清理。

答案 1 :(得分:0)

我建议使用

aidstring = Regex.Replace(aidstring, @"(""[^""\\]*(?:\\.[^""\\]*)*"")|\s+", "$1");

请参阅regex demo

正则表达式会将所有C引用的字符串与Capture组1匹配,并且$1这些字符串将在结果中恢复,但是\s+捕获的所有空格都将被删除。

正则表达式解释

备选方案1:

  • ("[^"\\]*(?:\\.[^"\\]*)*")
    • " - 文字"
    • [^"\\]* - 除\"
    • 以外的零个或多个字符
    • (?:\\.[^"\\]*)* - 零个或多个序列......
      • \\. - \以及任何字符,但换行符
      • [^"\\]* - 除\"
      • 以外的零个或多个字符
    • " - 文字"

备选方案2:

  • \s+ - 一个或多个空格(在.NET中,任何Unicode空格)
相关问题