I'm implementing validation for username field using Regular Expression(Regex) in iOS. I don't want username to start with the word "guest". I tried below code but it's not working.
[txtUserName addRegx:#"^(guest)" withMsg:#"Username Can't start with the word guest"];
Ideas?
You can try to use this Regex:
^(?!guest).*$
Explanation:
^ assert position at start of the string
(?!guest) Negative Lookahead - Assert that it is impossible to match the regex below
guest matches the characters guest literally (case sensitive)
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
$ assert position at end of the string
EDIT:
To make it case insensitive you can try this:
^(?i:(?!guest)).*$
You have to remove the ( ) like the following:
[txtUserName addRegx:#"^guest" withMsg:#"Username Can't start with the word guest"];
Related
I have following individual regular expressions I want to combine them using AND condition , I am using them for validating password
.[A-Z]+. - validate uppercase (one letter uppercase min)
.[0-9]+. - validate number ( one number atleast )
.[a-z]+. - validate lowercase ( one lower case minimum )
.{8,} - validate min character 8
.[^A-Za-z0-9]. - validate special character (atleast one special character )
(.)\1 - validate consecutive characters (no consecutive characters )
Right now I am validating every character separately , but i want to do it in one function only
I tried following way of combining
/^((.)\1)(.[A-Z]+.)(.[a-z]+.)(.[0-9]+.)(.[^A-Za-z0-9].).*$/
Above doesn't have all the expressions but I am trying to show how I have done.
One option is to use a set of positive lookaheads using negated character classes:
^(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?=[^0-9]*[0-9])(?!.*(.)\1)(?=[a-zA-Z0-9]*[^A-Za-z0-9\s])\S{8,}$
That will match:
^ Start of string
(?=[^A-Z]*[A-Z]) Assert uppercase
(?=[^a-z]*[a-z]) Assert lowercase
(?=[^0-9]*[0-9]) Assert digit
(?!.*(.)\1) Assert no consecutive char
(?=[a-zA-Z0-9]*[^A-Za-z0-9\s]) Assert char other than listed including a whitespace char (assuming that would not be allowed)
\S{8,} Match 8+ times a non whitespace char
$ End of string
Regex demo
Note that \S for the allowed chars is a broad match, you could specify what you would allow to match using a character class.
Within Rails, For a string that is a hashtag, I want to test that it is valid (alphanumeric and underscores only). But I don't know in advance whether the first character will be #
I could test for it:
if (string.first == '#')
string.match(/^[#][\w]+$/)
else
string.match(/^[\w]+$/)
How would I craft the regex to do the match but only allow the # character if it is the leading character?
There ? quantifier in regex, that means zero or one occurrence. So, your regexp transforms into:
string.match(/^#?\w+$/)
I'm trying to make a search feature that allows you to split a search into two when you insert a | character and search after what you typed.
So far I have understood how to keep the main command by capturing before the space.
An example being that if I type :ban user, a box below would still say :ban, but right when I type in a |, it starts the search over again.
:ba
:ba
:ban user|:at
:at
:ban user|:attention members|:kic
:kic
This code:
text=":ban user|:at"
text=text:match("(%S+)%s+(.+)")
print(text)
would still return ban.
I'm trying to get a match of after the final | character.
Then you can use
text=":ban user|:at"
new_text=text:match("^.*%|(.*)")
if new_text == nil then new_text = text end
print(new_text)
See the Lua demo
Explanation:
.* - matches any 0+ characters as many as possibl (in a "greedy" way, since the whole string is grabbed and then backtracking occurs to find...)
%| - the last literal |
(.*) - match and capture any 0+ characters (up to the end of the string).
To avoid special cases, make sure that the string always has |:
function test(s)
s="|"..s
print(s:match("^.*|(.*)$"))
end
test":ba"
test":ban user|:at"
test":ban user|:attention members|:kic"
I want to match strings of the forms:
123
.123
1.123
and I am using the following string for my regex
#"^\\d*(?:\\.\\d+)?$"
However, it matches strings of the following forms as well
1.2.3
1..2..3
123...
What's wrong with my regex? I used the ^ and $ because I don't want the string to contain anything other than the number forms mentioned.
EDIT:
I logged what is matched in the string like 78..7 and found that the match location is 0 and length is 0 with a result of "" being matched. Any ideas? Shouldn't the range location be NSNotFound if the length is 0? I suppose the regex expression is fine then and I can just check for !length but that seems like an unnecessary work around.
Try this regex:
^(?<!\.)\d*(\.\d+)?$
I added a negative look-behind assertion that means that no dot is allowed before that numbers. That should fix your problem.
Description
This regex will find valid positive real numbers with or without a decimal point. like 123, .123, 1.123. The expression can be applied against a string where each value tested is on it's own line or find numbers in the middle of a block of text. It will also allow punctuation like periods and commas directly after the number but won't capture them.
(?<=^|\s)\d*\.?\d+(?=[,.;]?(?:\s|$))
Given Input String:
1.2.3
1..2..3
128...
1234
.123
1.123
1...23
1.2.3
123...
I like kittens 345.23, and version 2.3.4 dogs
Matches are:
1234
.123
1.123
345.23
Does this work for you?
#"^\\d*\\.?\\d+$"
Here it is without escaped backslashes:
^\d*\.?\d+$
My best guess is that rekire is right about the $ symbol not working. If that's the case, then the regex does actually match the empty substring at the start of the string, which explains why it says it's found a match of length 0 at location 0, instead of NSNotFound.
This is REGEX match your strings:
[0-9]*(.){0,1}[0-9]+
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.