How do you write regular expressions for a valid username for swift?
^\w{1,13}$
^ beginning of string anchor
\w word character - any letter (including UTF-8), any case, 0-9 and underscore
{1,13} length between 1 and 13, inclusive
$ end of string anchor
https://regex101.com/ - good resource
How to write regular expression with global search in iOS?
what is \g
in regex? try @"[^A-Z]"
or @"[^A-Z]+"
with options 0
:
in iOS using regex to search or replace is really greedy, searching or replacing scopes is depend on range
in regex function's parameters.
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^A-Z]" options:0 error:&error]; // or @"[^A-Z]+"
NSString *UF = [regex stringByReplacingMatchesInString:@"New York" options:0 range:NSMakeRange(0, [@"New York" length]) withTemplate:@""];
Swift Regex with special Characters
The problem is that characters [
, \
, and ]
need to be escaped because they have special meaning in a regular expression.
So you need \[
, \\
, and \]
in the regular expression. But since this is inside a Swift string, each \
needs to be escaped with a \
.
So [\]
becomes \[\\\]
in the regular expression which becomes \\[\\\\\\]
in the Swift string.
The final valid string is:
var passwordRegex = "^[A-Za-z0-9 !\"#$%&'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~].{8,}$"
swift Regular Expression and (.*?)
Your exec
code contains .IgnoreMetacharacters
flag:
Treat the entire pattern as a literal string.
Remove it so that the pattern could be treated as a regex pattern.
Also, a good idea is to use a DOTALL modifier (?s)
at the start of the pattern.
Also, remember that a dot matches any character, escape it to match a literal dot.
As for the pattern, I'd recommend
"(?s)ytplayer\\.config = \\{(.*?)\\};"
Or a much faster:
"(?s)ytplayer\\.config = \\{([^}]*(?:\\}(?!;)[^}]*)*)\\};"
See the regex demo
How to Split String With Regular Expression ios objectivec
You may first replace all the matches with a non-used symbol, say, with \x00
null char, and then split with it:
NSError *error = nil;
NSString *str = @"LR00001";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=\\D)(?=\\d)" options:nil error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:str options:0 range:NSMakeRange(0, [str length]) withTemplate:@"\x00"];
NSArray *chunks = [modifiedString componentsSeparatedByString: @"\x00"];
for(NSString *c in chunks) {
NSLog(@"%@", c);
}
It prints LR
and 00001
.
See the online demo.
Regular Expressions in ios(a word a letter and special characters)
Description
^(?=.*[a-z])(?=.*[0-9])[a-z0-9!@$]*$
This regular expression will do the following:
(?=.*[a-z])
Require the string to contain at least onea-z
character(?=.*[0-9])
Require the string to contain at least one0-9
character[a-z0-9!@$]*$
Allow the string to be made up of onlya-z
characters,0-9
characters, and!@$
symbols
Example
Live Demo
https://regex101.com/r/mC3kL3/1
Explanation
NODE EXPLANATION
----------------------------------------------------------------------
^ the beginning of a "line"
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
[a-z] any character of: 'a' to 'z'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
[0-9] any character of: '0' to '9'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
[a-z0-9!]* any character of: 'a' to 'z', '0' to '9',
'!', '&', '#' (0 or more times (matching
the most amount possible))
----------------------------------------------------------------------
$ before an optional \n, and the end of a
"line"
----------------------------------------------------------------------
Related Topics
Apple Watch - Only Getting Data If App on Phone Is Active
Share Datas Between Two Apps with iOS 8 App Groups (Using Nsuserdefaults)
Perform Action by Clicking on Some Word in Uitextview or Uilabel
How to Only Override a Method Depending on the Runtime System iOS Version
Keep Bluetooth Sound When Initializing Avaudiosession
Avassetwriter Avvideoexpectedsourceframeratekey (Frame Rate) Ignored
How to Get Vcf Data with Contact Images Using Cncontactvcardserialization Datawithcontacts: Method
Handle JSON Response with Alamofire in Swift
Nsdateformatter Returns Nil in Swift and iOS Sdk 8.0
Uploads Using Backgroundsessionconfiguration and Nsurlsessionuploadtask Cause App to Crash
Cannot Show Modal Viewcontroller in iOS7
Get Sms Broadcast with Text Body Without Jailbreak But Private Frameworks in iOS
Why Does "Try Catch" in Objective-C Cause Memory Leak
Uitableviewcell Textlabel Color Not Changing
Firebase Queryorderedbychild() Method Not Giving Sorted Data