I have string:
Simple text with spaces
I need regular expression which select:
- leading
- trailing
- more than 1 space
Example:
_ - space
_Simple text __with ___spaces_
I have string:
Simple text with spaces
I need regular expression which select:
Example:
_ - space
_Simple text __with ___spaces_
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<
^\s+
and \s+$
match one or more white space characters at the start/end of the string.
The tricky part is the \s+(?=\s)
pattern, which matches one or more
white space characters followed by another white space character which itself is not considered
part of the match (a "look-ahead assertion").
Generally, \s
matches all white-space characters such as the space character itself, horizontal tabulation, newline, carriage-return, linefeed or formfeed. If
you want to remove only the (repeated) space characters then replace the pattern by
let pattern = "^ +| +$| +(?= )"
You can keep the regular expression simple by doing the leading/trailing part as a second stage:
let singlySpaced = " Simple text with spaces "
.stringByReplacingOccurrencesOfString("\\s+", withString: " ", options: .RegularExpressionSearch)
.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
(assuming you want to strip all kinds of whitespace – you can adjust it down to just do only spaces fairly easily)
There are more complex regexprs that will do it in one shot, but personally I prefer the two-step version over obfuscation (and as @MartinR mentions, performance is very similar between the two, given a trim is a very lightweight operation vs a slower more-complex regex - so it’s really down to which you prefer the look of).
This ought to clean your strings up:
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)
Some good answers have been provided, but if you want a regex the following should work:
^ |(?<= ) +| $
The |
indicates alternatives, the ^
is the beginning of a string, the $
indicates the end of the string. So this matches beginning of the string followed by a space OR one or more spaces preceded by a space OR a space at the end of the string.
Following will remove all the spaces
NSString *spaces =@"hi how are you ";
NSString *withoutSpace= [spaces stringByReplacingOccurrencesOfString:@" "
withString:@" "];
withoutSpace = [withoutSpace stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
this function will leave one space between words
let accountNameStr = " test test "
let trimmed = accountNameStr.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
let textWithoutSpace = trimmed.trimmingCharacters(in: .whitespaces)
Output : test test