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 .

643 questions
102
votes
8 answers

How to capture multiple repeated groups?

I need to capture multiple groups of the same pattern. Suppose, I have a following string: HELLO,THERE,WORLD And I've written a following pattern ^(?:([A-Z]+),?)+$ What I want it to do is, capture every single word, so that Group 1 is : "HELLO",…
phbelov
  • 1,519
  • 3
  • 13
  • 13
50
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
  • 10,932
  • 8
  • 84
  • 83
43
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
  • 10,562
  • 8
  • 46
  • 64
34
votes
10 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,135
  • 3
  • 21
  • 33
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,345
  • 15
  • 67
  • 113
14
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…
lakesh
  • 25,623
  • 57
  • 163
  • 254
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
  • 6,762
  • 9
  • 49
  • 80
10
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,529
  • 17
  • 32
10
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
  • 110,988
  • 21
  • 188
  • 277
10
votes
2 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
  • 109
  • 1
  • 1
  • 3
9
votes
8 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
  • 105
  • 1
  • 4
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
  • 87,949
  • 32
  • 210
  • 173
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
  • 971
  • 1
  • 8
  • 22
8
votes
2 answers

NSRegularExpression to extract text between two XML tags

How to extract the value "6" between the "badgeCount" tags using NSRegularExpression. Following is the response from the server:
Prazi
  • 335
  • 1
  • 5
  • 18
8
votes
2 answers

Is there an Objective-c regex replace with callback/C# MatchEvaluator equivalent?

I have a C# project I'm intending to port to Objective-C. From what I understand about Obj-C, it looks like there's a confusing variety of Regex options but I can't see anything about a way of doing a replace with callback. I'm looking for…
Michael Low
  • 23,431
  • 15
  • 75
  • 115
1
2 3
42 43