Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Regex in Objective-C [closed]

I'm always confused with Regex format how they work and how to make Regex according to my requirement. I just copy some common Regex formats and paste in my project but obviously we cant find every Regex format according to our requirement. So i would like to learn about Regex.

I make it just from my guess after run code again and again but still i dont know so much about this Regex

NSString *regexNumber = @"[123456789][0-9]{0}([0-9]{1})?";

This compare Age, according to this age should be start from 1-9, minimum 1 digit, maximum 2 digits and numeric only.

Now i want to make Regex for Name - (exp - vakul, Vakul, vakul saini, Vakul Saini, vakul Saini, Vakul saini etc.), Email, Phone Number, String Only, Birthday, URL. But dont want to copy and paste i want to learn how they work and how to make my own Regex.

like image 775
TheTiger Avatar asked Sep 24 '12 08:09

TheTiger


1 Answers

A regular expression is a text pattern consisting of a combination of alphanumeric characters and special characters known as metacharacters

The metacharacters are:

\ | ( ) [ { ^ $ * + ? . < >

. ---- It is instead a special metacharacter which matches any character.

* ---- The * character matches zero or more occurrences of the character in a row.

+ ---- The + character is similar to * but matches one or more.

? ---- Zero or one instance of characters

{m,n} ---- means match m or up to n characters. eg {1,5} matches 1 or upto 5 characters

^ ---- it would match any line which began with the word following it

$ --- it would match any line which ends with the word following it

<> --- matches words between them. Eg. returns all words that contain abc

You can form groups, or subexpressions as they are frequently called, by using the begin and end parenthesis characters: () The ( starts the subexpression and the ) ends it

| --- Or Parameter

[ and ] --- Sequence of characters. Any characters put inside the sequence brackets are treated as literal characters, even metacharacters. The only special characters are - which denotes character ranges, and ^ which is used to negate a sequence. eg. [a-z]

This is the most basic knowledge you need for regular expressions and they are same for almost all languages.

For further details you can refer this link

like image 171
DivineDesert Avatar answered Oct 03 '22 05:10

DivineDesert