Questions tagged [nsregularexpression]

The NSRegularExpression class is used to represent and apply regular expressions to Unicode strings. An instance of this class is an immutable representation of a compiled regular expression pattern and various option flags. The pattern syntax currently supported is that specified by ICU. iOS 4.0+ macOS 10.7+

This class in the Apple developer library (Mac OS X, iOS) is used to represent and apply regular expressions to Unicode strings.

NOTE: This tag should be used only for questions specific to the NSRegularExpression class. Questions about regular expressions in general should be tagged .

722 questions
416
votes
40 answers

How to validate an e-mail address in swift?

Does anyone know how to validate an e-mail address in Swift? I found this code: - (BOOL) validEmail:(NSString*) emailString { if([emailString length]==0){ return NO; } NSString *regExPattern =…
giorgionocera
  • 6,428
  • 6
  • 20
  • 17
64
votes
21 answers

Email & Phone Validation in Swift

i am using the following code for phone number validation. But i am getting the following error. I cant able to proceed further. Help us to take it forward. class PhoneNumberValidation: Validation { let PHONE_REGEX = "^\\d{3}-\\d{3}-\\d{4}$" …
Saikumar
  • 877
  • 1
  • 9
  • 22
52
votes
2 answers

How to write regular expressions in Objective C (NSRegularExpression)?

I have this regex working when I test it in PHP but it doesn't work in Objective C: (?:www\.)?((?!-)[a-zA-Z0-9-]{2,63}(?
budiDino
  • 13,044
  • 8
  • 95
  • 91
46
votes
4 answers

Capture groups not working in NSRegularExpression

Why is this code only spitting out the entire regex match instead of the capture group? Input @"A long string containing Name:A name here amongst other things" Output expected A name here Actual output Name:A name…
Maciej Swic
  • 11,139
  • 8
  • 52
  • 68
46
votes
11 answers

Check if string contains special characters in Swift

I have to detect whether a string contains any special characters. How can I check it? Does Swift support regular expressions? var characterSet:NSCharacterSet = NSCharacterSet(charactersInString:…
iPhone Guy
  • 1,285
  • 3
  • 23
  • 34
17
votes
2 answers

iOS Regex: Unknown escape sequence "\|"

I'm getting a weird warning, and as a result my regex search isn't working. Here's the line: NSRange r = [HTML rangeOfString:@"\|(.*)\|" options:NSRegularExpressionSearch]; Where HTML is a string that I'm sure contains a single match for the above…
Mason
  • 6,893
  • 15
  • 71
  • 115
15
votes
4 answers

Difference between \b and \s in Regular Expression

I was learning regular expression in iOS, saw this tutorial:http://www.raywenderlich.com/30288/nsregularexpression-tutorial-and-cheat-sheet It reads like this for \b: \b matches word boundary characters such as spaces and punctuation. to\b will…
lakshmen
  • 28,346
  • 66
  • 178
  • 276
13
votes
3 answers

Using regex to match date format in yyyymmdd

The regex should match valid dates in a string in the format YYYYMMDD. For example, aaa_20150327_bbb should be matched but aaa_20150229_bbb not because 2015 is not a leap year. Only year from 2000 to 2099 need to be considered.
Joe Wang
  • 139
  • 1
  • 1
  • 3
12
votes
5 answers

Using NSRegularExpression to extract URLs on the iPhone

I'm using the following code on my iPhone app, taken from here to extract all URLs from striped .html code. I'm only being able to extract the first URL, but I need an array containing all URLs. My NSArray isn't returning NSStrings for each URL, but…
neowinston
  • 7,584
  • 10
  • 52
  • 83
12
votes
3 answers

Named capture groups in NSRegularExpression - get a range's group's name

Apple says that NSRegularExpression is based on the ICU Regular Expression library: https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSRegularExpression_Class/ The pattern syntax currently supported is that specified by…
Dai
  • 141,631
  • 28
  • 261
  • 374
11
votes
1 answer

Non-greedy NSRegularExpression

I'm need an NSRegularExpression that matches non-greedily. You know, if there's: ABABABA ...and I ask it to match B.*B I want it to grab the SMALLEST possible match: BAB, not BABAB. I've been googling this for an hour now, and I keep finding…
baudot
  • 1,618
  • 20
  • 33
11
votes
7 answers

Regex Persian Date validation

I want a Regular Expression for validation Persian date like 1396/4/3, 1396/12/08 or something else. in other words, I want to ensure that format of Persian Date (as String) is some thing like these valid formats…
Morteza Asadi
  • 1,819
  • 2
  • 22
  • 39
11
votes
9 answers

Youtube Video Id from URL - Swift3

Basically I have a Youtube URL as string, I want to extract the video Id from that URL. I found some code in objective c that is as below: NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpression…
Susanta Sahu
  • 125
  • 1
  • 1
  • 5
9
votes
1 answer

How can I use NSRegularExpression on Swift strings with variable-width Unicode characters?

I'm having trouble getting NSRegularExpression to match patterns on strings with wider (?) Unicode characters in them. It looks like the problem is the range parameter -- Swift counts individual Unicode characters, while Objective-C treats strings…
Nate Cook
  • 92,417
  • 32
  • 217
  • 178
9
votes
2 answers

NSDate detection with NSDataDetector

I tried to get a NSDate from a NSString with UNKNOWN format, So I wrote a function like below -(void)dateFromString:(NSString*)string { NSError *error = NULL; NSDataDetector *detector = [NSDataDetector…
Bavan
  • 1,021
  • 1
  • 10
  • 24
1
2 3
48 49