regular expressions replace in iOS - ios

I am going to need to replace a dirty string for a clean string:
-(void)setTheFilter:(NSString*)filter
{
[filter retain];
[_theFilter release];
<PSEUDO CODE>
tmp = preg_replace(#"/[0-9]/",#"",filter);
<~PSEUDO CODE>
_theFilter = tmp;
}
This should eliminate all numbers in the filter so that:
#"Import6652"
would yield #"Import"
How can I do it in iOS ?
Thanks!

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:
#"([0-9]+)" options:0 error:nil];
[regex replaceMatchesInString:str options:0 range:NSMakeRange(0, [str length]) withTemplate:#""];
Swift
do {
let regex = try NSRegularExpression(pattern: "([0-9]+)", options: NSRegularExpressionOptions.CaseInsensitive)
regex.replaceMatchesInString(str, options: NSMatchingOptions.ReportProgress, range: NSRange(0,str.characters.count), withTemplate: "")
} catch {}

See the docs for NSRegularExpression
In particular the section titled Replacing Strings Using Regular Expressions

String replacing code using regex
Swift3
extension String {
func replacing(pattern: String, withTemplate: String) throws -> String {
let regex = try RegularExpression(pattern: pattern, options: .caseInsensitive)
return regex.stringByReplacingMatches(in: self, options: [], range: NSRange(0..<self.utf16.count), withTemplate: withTemplate)
}
}
use
var string: String = "Import6652"
do {
let result = try string.replacing(pattern: "[\\d]+", withTemplate: "")
} catch {
// error
}

in iOS 4.0+, you can use NSRegularExpression:
Use matchesInString:options:range: to get an array of all the matching results.
and then delete them from original string

Though not via a regular expression but you can use stringByTrimmingCharactersInSet
stringByTrimmingCharactersInSet:
Returns a new string made by removing from both ends of the receiver characters contained in a given
character set.
in combination with
decimalDigitCharacterSet
Returns a character set containing the characters in the category of Decimal Numbers.
like this:
[filter stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
Caveat: it will only remove the characters at both ends.

Related

remove string between parentheses [iOS]

i have a NSString with parentheses in it.
I would like to remove the Text inside of the parentheses.
How to do that? ( In Objective-C )
Example String:
Tach auch. (lockeres Ruhrdeutsch) Und Hallo!
I would like to Remove "(lockeres Ruhrdeutsch)" from the String,
but the Strings i have to edit are always different.
How can i remove the String betweeen "(" and ")"?
Best Regards
Use regular expression:
NSString *string = #"Tach auch. (lockeres Ruhrdeutsch) Und Hallo!";
NSString *filteredString = [string stringByReplacingOccurrencesOfString:#"\\(.*\\)"
withString:#""
options:NSRegularExpressionSearch range:NSMakeRange(0, string.length)];
NSLog(#"%#", filteredString);
If you want to consider also a whitespace character after the closing parenthesis, add \\s? to the end of the regex pattern.
Here is the function you can call to get your required string:
-(NSString*)getStringWithBlankParaFrom:(NSString*)oldStr{
NSArray*strArray1=[oldStr componentsSeparatedByString:#"("];
NSString*str2=[strArray1 objectAtIndex:1];
NSArray*strArray2 =[str2 componentsSeparatedByString:#")"];
NSString*strToReplace=[strArray2 objectAtIndex:0];
return [oldStr stringByReplacingOccurrencesOfString:strToReplace withString:#""];
}
This function is valid for the string which contains one pair of parentheses**()**
You can change it as per your requirement.
Hope this helps!

Replace occurrences of string pattern

So this is a string I get back from the server:
var arrayString = "IT34:1, IT35:2, IT36:1, IT35:3"
I want to get rid of any occurrences of ":1", ":2" and am using:
let cleanStr = arrayString.stringByReplacingOccurrencesOfString(":1", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
let cleanStr1 = cleanStr.stringByReplacingOccurrencesOfString(":2", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
And so on...
Which doesn't seem very efficient. Is it possible to accomplish this with just one .stringByReplacingOccurrencesOfString method? Like occurrences of ":(a number)"?
This can be done using regular expressions:
let string = "IT34:1, IT35:2, IT36:1, IT35:3"
var cleanStr = string
if let regex = NSRegularExpression(pattern: ":[0-9]", options:.CaseInsensitive, error: nil) {
cleanStr = regex.stringByReplacingMatchesInString(string, options: nil, range: NSMakeRange(0, countElements(string)), withTemplate: "")
}
println(cleanStr)
And in Objective-C:
NSString *string = #"IT34:1, IT35:2, IT36:1, IT35:3";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#":[0-9]" options:NSRegularExpressionCaseInsensitive error:nil];
NSString *newString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:#""];
NSLog(#"%#", newString);
I do not know the exact pattern of those ":1" thingies, but here's a solution you might fiddle with:
let cleanStr = ", ".join(arrayString.componentsSeparatedByString(", ").map { ($0 as NSString).substringToIndex(4) })
This solution will work as long as the ':1" things have one digit.
Here's a general solution:
let cleanStr = ", ".join(arrayString.componentsSeparatedByString(", ").map {
($0 as NSString).componentsSeparatedByString(":")[0] as String
})

Parsing strings: String before character

Input:
chris#mydomain.com
steve#mydomain.com
Output:
chris
steve
I'm looking to get the substring before the # character. In Python, I would use something like: myString = myString[:myString.find("#")] but I think Swift's version is very complex, at least from what I've been reading. Would it be better to bridge to Obj-C in this case, since Swift's indexOf/find function looks something like this mess: Finding index of character in Swift String ?
Something like this, even though it's using arrays, seems to be the simpliest route:
contact = contact.componentsSeparatedByString("#")[0]
In Swift, Python myString[:myString.find("#")] is equivalent to:
myString[myString.startIndex ..< find(myString, "#")!]
But this causes a runtime error unless myString contains "#".
This is safer.
if let found = find(myString, "#") {
myString = myString[myString.startIndex ..< found]
}
Note that, find() searches Character only, not substring. so you cannot:
find(myString, "#mydomain")
This may help you
let arr = split(contact, { $0 == "#"}, maxSplit: Int.max, allowEmptySlices: false)
println(arr[0])
I believe you have hit the nail on the head. Another option is to use regular expressions to find it. I'm still learning swift, but the objective-c version of things is as follows:
-(NSString*) parseEmailPrefix(NSString*)email{
NSError *error = nil;
NSString *pattern = #"([A-Z0-9a-z._%+-]+)#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern
options:0
error:&error];
NSTextCheckingResult *result = [expression firstMatchInString:email
options:0
range:NSMakeRange(0, email.length)];
return [string substringWithRange:[result rangeAtIndex:1]];
}
This is in my opinion harder to read, but it has the added benefit of validating the email as you go.
Regex credit: Regex for email address
And then there is this Obj-C snippet, which addresses more specifically the data given in the question. (Forgive that I have not translated to Swift, still working on that skill.)
NSString* email = #"chuck#norris.net";
NSURL* theURL = [NSURL URLWithString:[NSString stringWithFormat:#"mailto://%#", email]];
NSLog(#"%# at %#", theURL.user, theURL.host); // yields "chuck at norris.net"
This works:
var str: String = "Hello#World"
let a = str.componentsSeparatedByString("#")
println("\(a[0])")
Output is "Hello"

How to identify and remove newline and white spaces?

I am making an nsmutable array by separating a string by component it is causing a lot of new line and white spaces to be inserted in the array how to identify and remove them?
for (int i=0;i<contentsOfFile.count; i++)
{
if(!([[contentsOfFile objectAtIndex:i]isEqual:#"\n"]||[[contentsOfFile objectAtIndex:i]isEqual:#""]))
[arrayToBereturned addObject:[contentsOfFile objectAtIndex:i]];
}
this code which i am using cannot identify all new line charectors
thanks
To remove all extra space and \n from your string-
NSString* result = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
than prepare your contentsOfFile Array.
If you want an array without whitespace:
NSString *string = #"Hello, World!";
NSCharacterSet *separator = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSArray *stringComponents = [string componentsSeparatedByCharactersInSet:separator];
stringByTrimmingCharachersInSet: only removes desired characters from the end and the beginning of the string. To remove all occurences you should use stringByReplacingOccurrencesOfString:
Swift 5 version
let string = "Hello, stack overflow!"
let components = string.components(separatedBy: .whitespacesAndNewlines)
print(components) // prints ["Hello,", "stack", "overflow!"]
Also regarding string.replacingOccurrences
let string = " Hello, stack overflow ! "
let noSpacingsString = string.replacingOccurrences(of: " ", with: "")
print(components) // prints "Hello,stackoverflow!"

What characters are allowed in a iOS file name?

I'm looking for a way to make sure a string can be used as a file name under iOS. I'm currently in the section of the code that deletes incompatible characters. I'm wondering if I'm doing it right.
NSString *filename = #"A file name";
fileName = [fileName stringByTrimmingCharactersInSet: [NSCharacterSet controlCharacterSet]];
fileName = [fileName stringByTrimmingCharactersInSet: [NSCharacterSet newlineCharacterSet]];
I'm also wondering if there's already a method that validates a string as a file name.
Thank you for your advice!
Use RegEx:
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"[^a-zA-Z0-9_]+" options:0 error:nil];
filename = [regex stringByReplacingMatchesInString:filename options:0 range:NSMakeRange(0, filename.length) withTemplate:#"-"];
I find this to be cleaner and probably much more performant. This is based on Angel Naydenov's solution, but first constructing Character set with all invalid characters and then calling components(separatedBy:) just once.
Swift 3 & 4
var invalidCharacters = CharacterSet(charactersIn: ":/")
invalidCharacters.formUnion(.newlines)
invalidCharacters.formUnion(.illegalCharacters)
invalidCharacters.formUnion(.controlCharacters)
let newFilename = originalFilename
.components(separatedBy: invalidCharacters)
.joined(separator: "")
Swift 2
let invalidCharacters = NSMutableCharacterSet(charactersInString: ":/")
invalidCharacters.formUnionWithCharacterSet(NSCharacterSet.newlineCharacterSet())
invalidCharacters.formUnionWithCharacterSet(NSCharacterSet.illegalCharacterSet())
invalidCharacters.formUnionWithCharacterSet(NSCharacterSet.controlCharacterSet())
let filename = originalFilename
.componentsSeparatedByCharactersInSet(invalidCharacters)
.joinWithSeparator("")
First of all, you're using the wrong method. Trimming the string will only remove characters in the beginning and the end of the string.
What you're looking for is something more like:
fileName = [fileName stringByReplacingOccurrencesOfString:#"/" withString:#"_"];
However, that's a suboptimal solution, since you'll have to do that for every character you want to exclude, so maybe you want to keep looking or write you're own method for manipulating the string.
iOS is UNIX based and as such I suppose it supports almost any characters in filenames. UNIX allows white spaces, <, >, |, \, :, (, ), &, ;, as well as wildcards such as ? and *, to be quoted or escaped using \ symbol. However I wouldn't use any of those characters in my filenames. In fact, I would restrict the characters in my filenames to 'a'-'z', '0'-'9', '_' and '.'.
As I did not see a list with allowed characters in this question but the question wanted a list with such characters I am adding a bit more details on this topic.
First we need to know what is the file system that iOS devices use. Using multiple online sources this seems to be HFSX which is the HFS+ case sensitive version. And including one link here for reference: https://apple.stackexchange.com/questions/83671/what-filesystem-does-ios-use
Now that we know what the file system is we can look for what characters are not allowed. And these seem to be: colon (:) and slash (/). Here is a link for reference: http://www.comentum.com/File-Systems-HFS-FAT-UFS.html
Having this information and what others have written in this thread my personal preference for removing not allowed characters from file names is the following Swift code:
filename = "-".join(filename.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()))
filename = "-".join(filename.componentsSeparatedByCharactersInSet(NSCharacterSet.illegalCharacterSet()))
filename = "-".join(filename.componentsSeparatedByCharactersInSet(NSCharacterSet.controlCharacterSet()))
filename = "-".join(filename.componentsSeparatedByString(":"))
filename = "-".join(filename.componentsSeparatedByString("/"))
The reason I am not preferring the RegEx approach is that it seems too restrictive to me. I do not want to restrict my users only to Latin characters. They may as well wish to use some Chinese, Cyrillic or whatever else they like.
Happy coding!
I've had to save remote files locally with filenames containing other characters than basic alpha-numeric characters. I use the method below to strip out potential invalid characters, ensuring it's a valid filename for the filesystem when generating a NSURL using URLWithString:
filename = [[filename componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] componentsJoinedByString:#"" ];
filename = [[filename componentsSeparatedByCharactersInSet:[NSCharacterSet illegalCharacterSet]] componentsJoinedByString:#"" ];
filename = [[filename componentsSeparatedByCharactersInSet:[NSCharacterSet symbolCharacterSet]] componentsJoinedByString:#"" ];
fileURLString = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
fileURL = [NSURL URLWithString:fileURLString];
You may also want to test for collision errors first using:
[[NSFileManager defaultManager] fileExistsAtPath:[fileURL absoluteString]]
This String extension (Swift 4.2) will help convert an invalid iOS file name to a valid iOS file name.
extension String {
func convertToValidFileName() -> String {
let invalidFileNameCharactersRegex = "[^a-zA-Z0-9_]+"
let fullRange = startIndex..<endIndex
let validName = replacingOccurrences(of: invalidFileNameCharactersRegex,
with: "-",
options: .regularExpression,
range: fullRange)
return validName
}
}
For example
"name.name?/!!23$$#1asd".convertToValudFileName() // "name-name-23-1asd"
"!Hello.312,^%-0//\r\r".convertToValidFileName() // "-Hello-312-0-"
"/foo/bar/pop?soda=yes|please".convertToValidFileName() // "-foo-bar-pop-soda-yes-please"
I'm pretty happy with this solution:
NSString *testString = #"This*is::/legal.😀,?縦書き 123";
NSString *result = [[[testString componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"length > 0"]] componentsJoinedByString:#"-"];
Output:
"This-is-legal-縦書き-123"
What is this sorcery?
Let me break it up into multiple lines so it's clear what's going on:
NSString *testString = #"This*is::/legal.😀,?縦書き 123";
// Get a character set for everything that's NOT alphanumeric.
NSCharacterSet *nonAlphanumericCharacterSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
// Split the string on each non-alphanumeric character, thus removing them.
NSArray *cleanedUpComponentsWithBlanks = [testString componentsSeparatedByCharactersInSet:nonAlphanumericCharacterSet];
// Filter out empty strings ("length" is a KVO-compliant property that the predicate can call on each NSString in the array).
NSArray *cleanedUpComponentsWithoutBlanks = [cleanedUpComponentsWithBlanks filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"length > 0"]];
// Put the components back together and join them with a "-".
NSString *result = [cleanedUpComponentsWithoutBlanks componentsJoinedByString:#"-"];
Enjoy!
Swift 4 Version
Added by john-pang on 2021-09-01 with Swift version:
let testString = "This*is::/legal.😀,?縦書き 123"
// Get a character set for everything that's NOT alphanumeric.
let nonAlphanumericCharacterSet = CharacterSet.alphanumerics.inverted
// Split the string on each non-alphanumeric character, thus removing them.
let cleanedUpComponentsWithBlanks = testString.components(separatedBy: nonAlphanumericCharacterSet)
// Filter out empty strings ("length" is a KVO-compliant property that the predicate can call on each NSString in the array).
let cleanedUpComponentsWithoutBlanks = cleanedUpComponentsWithBlanks.filter { $0.length > 0 }
// Put the components back together and join them with a "-".
let result = cleanedUpComponentsWithoutBlanks.joined(separator: "_")
I came up with the following solution. Works nice so far.
import Foundation
extension String {
func removeUnsupportedCharactersForFileName() -> String {
var cleanString = self
["?", "/", "\\", "*"].forEach {
cleanString = cleanString.replacingOccurrences(of: $0, with: "-")
}
return cleanString
}
}
let a = "***???foo.png"
let validString = a.removeUnsupportedCharactersForFileName()
Base on Marian Answers, here is a string extension to remove any unwanted characters.
extension String {
func stripCharacters() -> String {
var invalidCharacters = CharacterSet(charactersIn: ":/")
invalidCharacters.formUnion(.newlines)
invalidCharacters.formUnion(.illegalCharacters)
invalidCharacters.formUnion(.controlCharacters)
let newString = self
.components(separatedBy: invalidCharacters)
.joined(separator: "_")
return newString
}
}
Example:
let fileName = "Man(lop23/45"
let newFileName = fileName.stripCharacters()
print(newFileName)
Swift 5 extension:
I wanted to remove emojis as well and in windows \ is also an invalid character. So I added symbols charset and backslash \ as well.
extension String {
var validFilename: String {
let invalidCharsets = CharacterSet(charactersIn: ":/\\")
.union(.illegalCharacters)
.union(.controlCharacters)
.union(.symbols)
.union(.newlines)
return self.components(separatedBy: invalidCharsets).joined()
}
}

Resources