Regular expression need to accept +, -, & before # - ios

regular expression need to accept +,-,& need to accept before # in email validation
([\w-\.]+)#((?:[\w]+\.)+)([a-zA-Z]{2,4}) in this regular expression its not accepting.
can any one provide me proper regular expression.
Example:
demo+wifi-mail&name#gmail.com

use this one it'l helps you.
([\\w-\\.\\+\\-\\&}]+)#((?:[\\w]+\\.)+)([a-zA-Z]{2,4})
NSString *phone=#"demo+wifi-mail&name#gmail.com";
NSString *pNRegex = #"([\\w-\\.\\+\\-\\&}]+)#((?:[\\w]+\\.)+)([a-zA-Z]{2,4})";
NSPredicate *PNTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", pNRegex];
BOOL check=[PNTest evaluateWithObject:phone ];
NSLog(#"%i",check);----> 1

This [\w\.] is a character class (I removed the - for now). Every character that is within the square brackets is matched by this class. So, your class is matching all letters, digits and underscores (that is done by the \w part) and dots.
If you want additional characters, just add them to the character class, e.g. [\w.+&-].
Be careful with the - character, it has a special meaning in a character class, either escape it or put it at the start or the end.
But be aware, your regex is still not matching all valid email addresses, see the links in the comments.
Single characters (without special meaning) or predefined classes written in a class doesn't make sense, [\w] is exactly the same than \w.

([\w-.+-\&}]+)#((?:[\w]+.)+)([a-zA-Z]{2,4}) Now it will work!

Related

Regexp: wildcard character with exceptions

I am not very familiar with regex. I am trying to match routes in ruby. I have a situation where I have some /route/:id that can take the 'id' parameter. I would like to match the route to any string with parameters as long as there is no forward slash. So anything like /route/123 should match but anything like /route/subroute/123 should not since there is a forward slash after 'subroute'. This is my current regex pattern that matches the '/routes/' portion and allows any string to take place of the 'id' parameter: \A\/routes\/\z*. This works, but if a forward slash is present in the 'id' portion of the route, the match still succeeds. What can I do to allow any string as the 'id' as long as a forward slash is not present?
This ended up being the pattern that worked for my case:
^\/route(\/[A-Za-z0-9\-\_]*)?$
Since it is a route, I found it better to allow only valid url characters and I use the parentheses (...)? for cases of routes that do not take parameters at all so '/route', '/route/', and '/route/abc' will all work, but '/route/abc/' will not.
In Ruby, ^ marks the start of any line, not string, so instead of ^, you need \A. Same with $, to match a string end position, you need to use \z.
Also, to match a single path subpart (the string between two slashes here) you can use a negated character class [^\/]+ / [^\/]*. If you plan to restrict the chars in the subpart to alphanumeric, hyphen and underscore, you can replace [^\/] with [\w-].
So, you can also use
/\A\/route(?:\/[\w-]*)?\z/
Details:
\A - start of string
\/route - a literal /route string
(?:\/[\w-]*)? - an optional (due to the last ?) non-capturing group that matches an optional sequence of / and then zero or more alphanumeric, underscore (\w) or hyphen chars
\z - end of string.
See the Rubular demo (here, ^ and $ are used for demo purposes only since the input is a single multiline text).

Non-reserved yet safe characters for delimiters in a URL

I have seen the following on StackOverflow about URL characters:
There are two sets of characters you need to watch out for - Reserved and Unsafe.
The reserved characters are:
ampersand ("&")
dollar ("$")
plus sign ("+")
comma (",")
forward slash ("/")
colon (":")
semi-colon (";")
equals ("=")
question mark ("?")
'At' symbol ("#").
The characters generally considered unsafe are:
space,
question mark ("?")
less than and greater than ("<>")
open and close brackets ("[]")
open and close braces ("{}")
pipe ("|")
backslash ("\")
caret ("^")
tilde ("~")
percent ("%")
pound ("#").
I'm trying to code a URL so I can parse it using delimiters. They can't be numbers or letters though. Does anyone have a list of characters that are NOT Reserved but ARE safe to use?
Thanks for any help you can provide.
Don't bother trying to use safe/unreserved characters. Just use whatever delimiters you want and URLencode the whole thing. Then URL decode it on the other end and parse normally.
Is there a reason you can't just use the standard delimiter for URL parameters (&)? That is the most straightforward way to do it instead of trying to roll your own.
For example the standard URL syntax already allows for multi-valued paramaters natively. This is perfectly legal and doesn't require any trickery.
Somepage.aspx?parameterName=A&parameterName=B
The result is that the page would be passed "A,B" in the parameterName attribute.

username regex in rails

I am trying to find a regex to limit what a person can use for a username on my site. I don't need to have it check to see how many characters there are in it, as another validation does this. Basically all I need to make it do is make sure that it allows: letters (capital and lowercase) numbers, dashes and underscores.
I came across this: /^[-a-z]+$/i
But it doesn't seem to allow numbers.
What am I missing?
The regex you're looking for is
/\A[a-z0-9\-_]+\z/i
Meaning one or more characters of range a-z, range 0-9, - (needs to be escaped with a backslash) and _, case insensitive (the i qualifier)
Use
/\A[\w-]+\z$/
\w is shorthand for letters, digits and underscore.
\A matches at the start of the string, \z matches at the end of the string. These tokens are called anchors, and Ruby is a bit special with regard to them: Most regex engines use ^ and $ as start/end-of-string anchors by default, whereas in Ruby they can also match at the start/end of lines (which matters if you're working with multiline strings). Therefore, it's safer (as #JustMichael pointed out) to use \A and \z because there is no such ambiguity.
Your regular expression contains a character class [-a-z] that allows the characters - (dash) and a through z. In order to expand the range of characters allowed by this character class, you will need to add more characters within the [].
Please see Character Classes or Character Sets for further information and examples.

Regex to check consecutive occurrence of period symbol in username

I have to validate username in my app so that it cannot contain two consecutive period symbols. I tried the following.
username.match(/(..)/)
but found out that this matches "a." and "a..". I expected to get nil as the output of the match operation for input "a.". Is my approach right ?
You need to put a \ in front of the periods, because period is a reserved character for "any character except newline".
So try:
username.match(/(\.\.)/)
Short answer
You can use something like this (see on rubular.com):
username.match(/\.{2}/)
The . is escaped by preceding with a backslash, {2} is exact repetition specifier, and the brackets are removed since capturing is not required in this case.
On metacharacters and escaping
The dot, as a pattern metacharacter, matches (almost) any character. To match a literal period, you have at least two options:
Escape the dot as \.
Match it as a character class singleton [.]
Other metacharacters that may need escaping are | (alternation), +/*/?/{/} (repetition), [/] (character class), ^/$ (anchors), (/) (grouping), and of course \ itself.
References
regular-expressions.info/Literals and metacharacters, Dot: ., Character Class: […], Anchors: ^$, Repetition: *+?{…}, Alternation: |, Optional: ?, Grouping: (…)
On finite repetition
To match two literal periods, you can use e.g. \.\. or [.][.], i.e. a simple concatenation. You can also use the repetition construct, e.g. \.{2} or [.]{2}.
The finite repetition specifier also allows you write something like x{3,5} to match at least 3 but at most 5 x.
Note that repetition has a higher precedence that concatenation, so ha{3} doesn't match "hahaha"; it matches "haaa" instead. You can use grouping like (ha){3} to match "hahaha".
On grouping
Grouping (…) captures the string it matches, which can be useful when you want to capture a match made by a subpattern, or if you want to use it as a backreference in the other parts of the pattern.
If you don't need this functionality, then a non-capturing option is (?:…). Thus something like (?:ha){3} still matches "hahaha" like before, but without creating a capturing group.
If you don't actually need the grouping aspect, then you can just leave out the brackets altogether.

a simple regexp validator

How do a I create a validator, which has these simple rules. An expression is valid if
it must start with a letter
it must end with a letter
it can contain a dash (minus sign), but not at start or end of the expression
^[a-zA-Z]+-?[a-zA-Z]+$
E.g.
def validate(whatever)
reg = /^[a-zA-Z]+-?[a-zA-Z]+$/
return (reg.match(whatever)) ? true : false;
end
/^[A-Za-z]+(-?[A-Za-z]+)?$/
this seems like what you want.
^ = match the start position
^[A-Za-z]+ = start position is followed by any at least one or more letters.
-? = is there zero or one hyphens (use "*" if there can be multiple hyphens in a row).
[A-Za-z]+ = hyphen is followed by one or more letters
(-?[A-Za-z]+)? = for the case that there is a single letter.
$= match the end position in the string.
xmammoth pretty much got it, with one minor problem. My solution is:
^[a-zA-Z]+\-?[a-zA-Z]+$
Note that the original question states, it can contain a dash. The question mark is needed after the dash to make sure that it is optional in the regex.
^[A-Za-z].*[A-Za-z]$
In other words: letter, anything, letter.
Might also want:
^[A-Za-z](.*[A-Za-z])?$
so that a single letter is also matched.
What I meant, to be able to create tags. For example: "Wild-things" or "something-wild" or "into-the-wild" or "in-wilderness" "my-wild-world" etc...
This regular expression matches sequences, that consist of one or more words of letters, that are concatenated by dashes.
^[a-zA-Z]+(?:-[a-zA-Z]+)*$
Well,
[A-Za-z].*[A-Za-z]
According to your rules that will work. It will match anything that:
starts with a letter
ends with a letter
can contain a dash (among everything else) in between.

Resources