FAQ Database Discussion Community


Regex validation in Cocoa Touch

ios,regex,cocoa-touch,nsregularexpression
Is there an equivalent to pattern.compile() for regexes in Cocoa Touch? Is it possible to check if the given regex expression is error-free? Will NSRegularExpression's NSMatchingInternalError handle this test case?

Can't create NSRegularExpression because of “Couldn't create (Metatype) escape NSRegularExpression”

nsregularexpression
I'm trying to create the following NSRegularExpression (I'm trying to find all commas ,, left-parens (, right-parens ), and backslashes \): var error: NSError? NSRegularExpression(pattern: "[,\\(\\)\\]", options: nil, error: &error) But it returns .None with the following error: Couldn't create (Metatype) escape NSRegularExpression And the following userInfo: NSInvalidValue: "[,\(\)\]" What...

Possible bug in NSRegularExpression (optional atom)…?

regex,nsregularexpression
As part of a web service response, I'm having to extract date strings that so far have taken the either of these formats: 06 Mar 2015-10:24 EST 06 Mar 2015-10:24 (I've got no control over the service itself; there's a whole assortment of nonstandard date formats there plus [inaccurate] localization,...

How to remove 100s of warnings “implicit conversion loses integer precision: 'NSInteger' (aka 'long') to 'int'” I got after updating to arm64?

objective-c,compiler-warnings,nsregularexpression,arm64,xcode6.3
Problem: Yesterday I converted a large project of mine to support arm64 and after that I got 500+ warnings at once. About 70% of them are where NSInteger is being assigned to int or vice versa, and remaining are where NSUInteger is formatted in NSString like this: NSInteger a =...

Find range of “goals” that aren't in comments with Regex

regex,nsregularexpression
Maybe this is simple, but I haven't found a way to achieve the following behavior of regex. I want to match substrings that starts with {! and end with !}. I've tried the following regex: {!.*!}. But I want to exclude those, that starts with line --. For example, let's...

Java regex _ text between tags?

java,regex,nsregularexpression
How I can get Stringfrom between tags, I have : <span style="color: rgb(51, 102, 255);"><br>AAA </span> <span style="color: rgb(51, 102, 255);">BBB : </span> <span style="color: rgb(51, 102, 255);"><br>CCC : </span> I need to : <br>AAA BBB : <br>CCC I wrote this but don't work : String _comment = "*my html*"...

Python match whole file name, not just extension

python,regex,nsregularexpression
Objective: Use Python regex to return a list of filenames have certain file extensions. List of files: X8SIA9.ROM X8SIA0.rom X8SIA0.928 Regex to find files that end in *.ROM or 3x numbers: bios_file = re.findall(r'.*\.(rom|[0-9]{3})+', name, re.I) Issue: bios_file is returning ['ROM'], ['rom'], [928]. bios_file should be returning the entire filename...

How to remove comment HTML element in a String in iOS

html,ios,swift,pattern-matching,nsregularexpression
This is the last part of my String: </div> </noscript><!-- A g3.js-t oldalanként egyszer, a </body> zárótag előtt kell meghívni --> <script type="text/javascript" charset="utf-8" src="//ad.adverticum.net/g3.js"></script> <div id="autosuggest"><ul></ul></div> </body> </html> And this is how I want to remove the comment element, but it does not work: var regex = NSRegularExpression(pattern: "<!--[^<]*-->",...

Regular Expression match special colon

regex,nsregularexpression
I have the following string : string x = "abc<ee:dd>\r\nHERE:we<pp>xq:zxcv<qq>"; I want to write a regular expression to find the first : which is not in a <> block. Ideally, I want to get the parts before and after the first : That is: string y ="abc<ee:dd>\r\nHERE" string z =...

How can I replace string using NSRegularExpression in iOS?

ios,objective-c,regex,swift,nsregularexpression
For example the regular expression is : A(B)C A,B,C all represent some string.I want all the string matches A(B)C replacing by B. If the NSString is AABCAABCBBABC: The answear will be ABABBBB.How to do that? Thank you. I give a more specific example: <script\stype="text/javascript"[\s\S]*?(http://[\s\S]*?)'[\s\S]*?</script> The answer is some script mathes...

Regex in iOS to limit a text field to only letters and single apostrophies

ios,regex,nsregularexpression
After a text field is returned I want to check if the text provided is valid. Valid means only letters or a single apostrophe, for names. I'm pretty new to regular expressions. Is there a simple regular expression I can use for this check, or can someone point me towards...

What sort of regex is this?

ios,regex,nsregularexpression
I just found someone using a regex like this: NSString *pattern = @"i[\\p{Alphabetic}&&\\p{Uppercase}][\\p{Alphabetic}]+"; [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:NULL]; // Source: http://www.objc.io/issue-5/getting-to-know-textkit.html // Tested. It actually works. The regular expression looks pretty cool and readable (supposedly more compatible with Unicode). Where can I find more info on this kind of regex? Is...

Extract text between two markers with Regular Expression

ios,regex,nsregularexpression
Simple regex question. I have a string on the following format: [page] Some text with multi line. [page/] [page] Another text with multi line. [page/] [page] Third text with multi line. [page/] What is the regular expression to extract the text between [page] and [page/]? I am using this code,...

NSRegularExpression - Match version number

ios,regex,swift,nsregularexpression
In order to check if a string is a version number thanks to NSRegularExpression, I created the following pattern: let pattern = "^(\\d+)(?:\\.(\\d+))?(?:\\.(\\d+))?(?:\\.)?$" It works pretty well and allows me to match: "8", "12.", "8.3", "8.10.", "8.2.5", and "8.2.28.". However, when I try to improve it with the following pattern,...

How to replace strings using regex in objective C?

ios,objective-c,regex,nsregularexpression
I have a string that sometimes contains strings like: @"[id123123|Some Name]" What I have to do, is to simply replace it to "Some Name" For example I have string: Some text lalala blabla [id123|Some Name] bla bla bla And I need to get: Some text lalala blabla Some Name bla...

Double \\ in regular expression iOS

ios,regex,nsregularexpression
Does anyone understand what this (([A-Za-z\\s])+)\\? means? I wonder why it should be "\\s" and "\\" ? If I entered "\s", Xcode just doesn't understand and if I entered "\?", it just doesn't match the "?". I have googled a lot, but I did not find a solution. Anyone knows?...

Find all HTML sibling element in iOS

html,ios,regex,nsregularexpression
I have a huge HTML, but at a certain level there are 10 piece of article element. I need theme. <article class="box-product-big box-product-full clearfix" > <div class="list-left"> <div class="cover"> <a id="book_cover_3100529" href="/film/fritz_lang.m-egy-varos-keresi-a-gyilkost-dvd.html"> <img src="http://s06.static.libri.hu/cover/d4/3/1090228_3.jpg" alt="Fritz Lang - M- Egy város keresi a gyilkost - DVD"/> </a> </div> <div class="desc"> <a...

Regular Expression not working in iOS with $ character

ios,regex,nsregularexpression
I am new to RegularExpressions in iOS . I am using Regular Expression in some part of my code but its not working . Correct me if I am doing Something wrong I am calling the compareStringWithRegex function like this [self compareStringWithRegex:@"Web" [email protected]"eb$"]; Method - (BOOL)compareStringWithRegex:(NSString *)string withRegexPattern:(NSString *)expression {...

Regular expression to find and replace hyphened class name with a fixed suffix.

regex,nsregularexpression
I have this type of divs which similar class , now I wanted to replace all the class with "-type" at the end with regular expression. I have tried this so far http://regexr.com/3b7q3. <div class="form-group form-row @styleProperty username-v-type" data-type="@templateName" data-prop="@styleProperty" data-is-validate="@isValidate"> </div> ...

Remove string after the hyphen

ios,objective-c,regex,nsregularexpression
I have multiple strings written at the same way: Name-372198 Another-9849204 Something-3439483 I want to split the string into two parts: first one before the hyphen and the other after the hyphen. I'm stuck with Objective-c Regular Expression!...

Ignore escaped double quote characters swift

regex,swift,escaping,nspredicate,nsregularexpression
I am trying to validate a phone number using NSPredicate and regex. The only problem is when setting the regex Swift thinks that I am trying to escape part of it due to the backslashes. How can I get around this? My code is as follows: let phoneRegEx = "^((\(?0\d{4}\)?\s?\d{3}\s?\d{3})|(\(?0\d{3}\)?\s?\d{3}\s?\d{4})|(\(?0\d{2}\)?\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$"...

How to fix the number of repeat some string in regular expression?

nsregularexpression
On this link, I see this expression: ba{2,4} will match baa, baaa and baaaa. What if I want to repeat not only single character, but also a string? For example, I want to catch matches like the following. a3a/bBb/ or aaa/bbb/c3c/ This doesn't work. [a-z|A-Z|0-9]+/{2,3} Thanks in advance....

iOS regular expression arabic

ios,regex,localization,arabic,nsregularexpression
I come from this posts Regular Expression Arabic characters and numbers only How to match arabic words with reg exp? (didn't answer my question) I've tried ^[\p{Arabic} ]+$ and received 'Parse error', reason: 'Invalid escape sequence @ pos 3: ^[\ ▶p{Arabic} ]+$' I've also tried ^[\u0621-\u064A\s]+$ 'Parse error', reason: 'Invalid...

Using capture groups within an NSRegularExpression pattern

ios,objective-c,regex,swift,nsregularexpression
Is a regex of the following form legit in Obj C? "<(img|a|div).*?>.*?</$1>" I know it's valid in JS with a \1 instead of $1, but I'm having little luck in Obj C. ...

How do I repeat a capturing group?

ios,regex,nsregularexpression
I have an input string that looks something like this: HLI6Ch60000Ch500C0Ch46400Ch30000Ch21888Ch10E79CS07LCU3Ch37880Ch27800Ch16480CS8CA00000000000000000000 Now I don't care about the part that follows the last letter A, it'll always be A and exactly 20 numbers that are of no use to me. I do, however, need the part before the last letter A,...