Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从字符串中去掉前导、尾随和超过1个空格_Ios_Regex_String_Swift_Nsregularexpression - Fatal编程技术网

Ios 从字符串中去掉前导、尾随和超过1个空格

Ios 从字符串中去掉前导、尾随和超过1个空格,ios,regex,string,swift,nsregularexpression,Ios,Regex,String,Swift,Nsregularexpression,我有字符串: Simple text with spaces 我需要正则表达式,它可以选择: 领先的 拖尾 超过1个空间 例如: _ - space _Simple text __with ___spaces_ 下面将删除所有空格 NSString *spaces =@"hi how are you "; NSString *withoutSpace= [spaces stringByReplacingOccurrencesOfString:@" "

我有字符串:

Simple text   with    spaces
我需要正则表达式,它可以选择:

  • 领先的
  • 拖尾
  • 超过1个空间
例如:

_ - space

_Simple text __with ___spaces_

下面将删除所有空格

NSString *spaces =@"hi how are you "; 

NSString *withoutSpace= [spaces stringByReplacingOccurrencesOfString:@"  " 
                               withString:@" "];
withoutSpace = [withoutSpace stringByTrimmingCharactersInSet:
                              [NSCharacterSet whitespaceCharacterSet]];

您可以通过将前导/尾随部分作为第二阶段来保持正则表达式的简单性:

 let singlySpaced = " Simple text   with    spaces   "
      .stringByReplacingOccurrencesOfString("\\s+", withString: " ", options: .RegularExpressionSearch)
      .stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
(假设您想要去除所有类型的空白–您可以将其调低到只相当容易地处理空格)


有更复杂的regexpr可以一次性完成,但我个人更喜欢两步版本而不是模糊处理(正如@MartinR提到的,两者的性能非常相似,因为修剪是一个非常轻量级的操作,而不是更复杂的较慢的regex-所以你更喜欢它的外观).

这应该可以清理你的字符串:

var string : NSString = "  hello    world.  "
while string.rangeOfString("  ").location != NSNotFound { //note the use of two spaces
    string = string.stringByReplacingOccurrencesOfString("  ", withString: " ")
}
println(string)
string = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
println(string)

已经提供了一些很好的答案,但是如果您想要一个正则表达式,以下内容应该可以:

^|(?My 2ct:

let text = "    Simple text   with    spaces    "
let pattern = "^\\s+|\\s+$|\\s+(?=\\s)"
let trimmed = text.stringByReplacingOccurrencesOfString(pattern, withString: "", options: .RegularExpressionSearch)
println(">\(trimmed)<") // >Simple text with spaces<

此函数将在单词之间留出一个空格

let accountNameStr = "   test    test   "
    let trimmed = accountNameStr.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
    let textWithoutSpace = trimmed.trimmingCharacters(in: .whitespaces)

输出:test test

请正确解释问题。这将用于删除所有空格,但我相信他希望留下一些空格。他似乎希望在单词之间留下1个空格。抱歉,请检查更新的答案…他希望删除、尾随、前导和多个空格,这将完成相同的操作,即我会的!另一件事是问题中列出的语言是Swift,您的代码是Objective-C。虽然不难转换,但它仍然是不匹配的。也许不是,我没有否决它,所以这取决于谁。可能存在仅使用stringByReplacingOccurrencesOfString方法的方式。示例:let singlySpaced=“带空格的简单文本”。stringByReplacingOccurrencesOfString(“正则表达式”,带字符串:“”,选项:。RegularExpressionSearch)。正则表达式将选择前导、尾随和多个空格?另外,我需要节省执行时间。很容易同时匹配前导/尾随/多个内容(如Robert的回答所示)使用
^ |+|$
。棘手的部分是,如果要删除,则要用什么来替换它–您要用零替换前导/尾随,但要用单个空格替换多个内部空格。Thx,位“+”表达式,选择单词之间的所有空格。我需要从出现的第二个空格开始选择,以结束空格组.Example
word\uuuu-word
。您需要一个查找后面的断言。更改了我的答案。如果您只想匹配一个空格、一个制表符、一个换行符或一个换行符,那么很有兴趣知道对于各种可能的字符串,这是否优于两步执行法,因为更复杂的正则表达式可能更慢。请注意:正则表达式
\s
匹配一个空格、一个制表符、一个换行符或一个换行符给我们一个真实的空间space@AirspeedVelocity:无显著差异。对于1MB文本:您的方法:0.572秒,我的方法:0.502秒。
let accountNameStr = "   test    test   "
    let trimmed = accountNameStr.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
    let textWithoutSpace = trimmed.trimmingCharacters(in: .whitespaces)