如何在Sublime Package Development YAML tmlanguage中匹配运算符分隔的字符串

时间:2016-04-15 12:15:50

标签: regex string sublimetext2 yaml tmlanguage

我正在使用PackageDevelopment的 .YAML-tmLanguage 为sublime text 2中的自定义语言创建语法定义。现在我希望我的语法能够识别非字符串的字符串。

代码行代码:

string name = "Chuck Norris";
string message = "I am " + name + ", don't mess with a \"ROCKSTAR\"!";

我的双引号字符串模式:

- comment: strings in double quotes
  match: (".+")
  captures:
    '1': {name: string.quoted.double.me}

模式捕获的内容:

字符串名称= "Chuck Norris";
string message = "I am " + name + ", don't mess with a "ROCKSTAR"!";

上面的第1行是正确的,但第2行似乎捕获了所有。

我想要的是:

字符串名称= "Chuck Norris";
字符串消息= "I am " +名称+ ", don't mess with a "ROCKSTAR"!";

2 个答案:

答案 0 :(得分:1)

您需要匹配所有不是import UIKit class HomepageCollectionViewController: UICollectionViewController { var imageCache = NSCache() var hingeImagesArray = [HingeImage]() var arrayToHoldConvertedUrlToUIImages = [UIImage]() var task: NSURLSessionDataTask? override func viewDidLoad() { super.viewDidLoad() // Makes the network call for HingeImages refreshItems() } override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 1 } override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return hingeImagesArray.count } override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("imageReuseCell", forIndexPath: indexPath) as! ImageCollectionViewCell let image = hingeImagesArray[indexPath.row] if let imageURL = image.imageUrl { if let url = NSURL(string: imageURL) { //settingImageTpChache if let myImage = imageCache.objectForKey(image.imageUrl!) as? UIImage { cell.collectionViewImage.image = myImage }else { // Request images asynchronously so the collection view does not slow down/lag self.task = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data, response, error) -> Void in // Check if there is data returned guard let data = data else { return } // Create an image object from our data and assign it to cell if let hingeImage = UIImage(data: data){ //cachedImage self.imageCache.setObject(hingeImage, forKey: image.imageUrl!) dispatch_async(dispatch_get_main_queue(), { () -> Void in cell.collectionViewImage.image = hingeImage //append converted Images to array so we can send them over to next view - only proble in that the only images converted at the ones you scrool to which is retarted self.arrayToHoldConvertedUrlToUIImages.append(hingeImage) print(self.arrayToHoldConvertedUrlToUIImages) }) } }) task?.resume() } } } return cell } 的字符以及" +组合之间的所有字符。

使用

\

请参阅this regex demo

它也可以写成"[^"\\]*(?:\\.[^"\\]*)*" 。它更容易阅读,但效率较低。

答案 1 :(得分:1)

我发现了一个关于如何匹配运算符分隔的字符串以及使用YAML匹配双引号字符串和一些其他功能的良好实现。

@WiktorStribiżewv的回答也解决了这个问题,但我找到了一个很好的实现:

- comment: strings in double quotes
  name: string.quoted.double.hit
  begin: \"
  end: \"
  patterns:
  - comment: escape characters
    name: constant.character.escape.hit
    match: \\.

  - comment: special characters
    name: constant.character.hit
    match: \%.

这也匹配转义字符,例如\"\n和特殊字符%s%d