Regex to allow alphanumeric characters and should allow . (dot) ' (apostrophe) and - (dash) - ruby-on-rails

I am trying to build a Regex to validate name of the user which will be contain alphanumeric characters and should allow . (dot) ' (apostrophe) and - (dash), I have tried with following regex, but they are not working
/^[\w-'.]$/
/^[a-zA-Z0-9\.'-]$/
Please help

A few things were missing:
Escape the last dash in the set. The - symbol denotes a range in a set, such as with a-z.
After the set add +, so that the characters are matched one or more times.
Expression
^[a-zA-Z0-9\.'\-]+$
REY
You could also revise it to something like ^[a-zA-Z0-9\.'\-]{5,}$, where the {5,} requires a minimum of 5 members of the set matched concurrently. Usually user names have to be longer than 1 character.

Related

Regex for iOS Bundle ID in platform for application registered in Azure B2C

I need a regex with max 222 chars
must use a-z A-Z 0-9
can also have . and -
but:
can not start from . or -
can not end with . or -
can not have multiple like ".." or "--"
can not have only . or -
Examples:
GOOD:
asd.asd.asd.asd.asd.aadas.asdasdasd
as-d.asd.a-sd
BAD:
.asd.asd
-asd.-asd
.-asd.asd
asd.asd.
asd.asd-
asd.asd.-
asd.asd-.
asd.asd--
asd..
asd.asd-.asd
.-
asd--asd..asd
I ended up with something like this
^[^-.]((?!--)[a-zA-Z0-9\-]\.?[^-]){1,100}[^-.]$
but it does not cover this case like asd.asd-.asd
this is regex for platform iOS Bundle ID in app registration.
You might write it without the hyphen in the character class [a-zA-Z0-9\-]
(Note that if the - is at the start or end, you don't have to escape it)
^(?=.{1,100}$)[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*$
^ Start of string
(?=.{1,100}$) Assert 1-100 chars
[a-zA-Z0-9]+ Repeat 1+ times matching any of the listed
(?:[-.][a-zA-Z0-9]+)* Optionally repeat either - or . and repeat 1+ times any of the listed
$ End of string
Regex demo

What does this pattern ^[%w-.]+$ mean in Lua?

Just came across this pattern, which I really don't understand:
^[%w-.]+$
And could you give me some examples to match this expression?
Valid in Lua, where %w is (almost) the equivalent of \w in other languages
^[%w-.]+$ means match a string that is entirely composed of alphanumeric characters (letters and digits), dashes or dots.
Explanation
The ^ anchor asserts that we are at the beginning of the string
The character class [%w-.] matches one character that is a letter or digit (the meaning of %w), or a dash, or a period. This would be the equivalent of [\w-.] in JavaScript
The + quantifier matches such a character one or more times
The $ anchor asserts that we are at the end of the string
Reference
Lua Patterns
Actually it will match nothing. Because there is an error: w- this is a start of a text range and it is out of order. So it should be %w\- instead.
^[%w\-.]+$
Means:
^ assert position at start of the string
[%w\-.]+ match a single character present in the list below
+ Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
%w a single character in the list %w literally (case sensitive)
\- matches the character - literally
. the literal character .
$ assert position at end of the string
Edit
As the OP changed the question and the tags this answer no longer fits as a proper answer. It is POSIX based answer.
As #zx81 comment:
%w is \w in Lua which means any alphanumeric characters plus "_"

How do you make a better seo friendly URL when there is UTF-8 characters and many space in between?

When someone by mistake enters many spaces between characters what I do is to replace all spaces with - but what if there are many spaces in between? for e.g:
User entered post title:
فارسی * Allposts---
When I convert the above example to user-friendly url (slug) I get this:
----فارسی---*-Allposts---
How to put one - for spaces and remove special characters and preserve utf-8 characters as well? The output I'm seeking for is as below:
فارسی-Allposts
Is there a way to handle it with regex? if positive, how?
EDIT:
Now I can manage multiple spaces as below:
$string = preg_replace('/\s+/', '-', $string);
but for special chars problem still remains.
Remove special characters: replace [\-\?\*] or whatever your blacklist characters are with empty string.
Convert strings of whitespace to a single - character: replace \s+ with -
Looks like you already figured out step 2. Make sure you do it second so you don't accidentally remove your own hyphens that you just inserted.

How to update this REGEX to make sure string does not have _(underscore) at the end or beigning

This is the regular expression which i have, i need to make sure that string does not start or end with underscore , underscore may appear in between.
/^[a-zA-Z0-9_.-]+$/
I have tried
(?!_)
But doesn't seem to work
Allowed strings:
abcd
abcd_123
Not allowed strings:
abcd_
_abcd_123
Not too hard!
/^[^_].*[^_]$/
"Any character except an underscore at the start of the line (^[^_]), then any characters (.*), then any character except an underscore before the end of the line ([^_]$)."
This does require at least two characters to validate the string. If you want to allow one character lines:
/^[^_](.*[^_]|)$/
"Anything except an underscore to start the line, and then either some characters plus a non-underscore character before end-of-line, or just an immediate end-of-line.
You could approach this in the inverse way,
Check all those that do match starting and ending underscores like this:
/^_|_$/
^_ #starts with underscore
| #OR
_$ #ends with underscore
And then eliminate those that match. The above regexp is much more easier to read.
Check : http://www.rubular.com/r/H3Axvol13b
Or you can try the longer regex:
/^[a-zA-Z0-9.-][a-zA-Z0-9_.-]*[a-zA-Z0-9.-]$|^[a-zA-Z0-9.-]+$|^[a-zA-Z0-9.-][a-zA-Z0-9.-]$/
^[a-zA-Z0-9.-] #starts with a-z, or A-Z, or 0-9, or . -
[a-zA-Z0-9_.-]* #anything that can occur and the underscore
[a-zA-Z0-9.-]$ #ends with a-z, or A-Z, or 0-9, or . -
| #OR
^[a-zA-Z0-9.-]$ #for one-letter words
| #OR
^[a-zA-Z0-9.-][a-zA-Z0-9.-]$ #for two letter words
Check: http://www.rubular.com/r/FdtCqW6haG
/^[a-zA-Z0-9.-][a-zA-Z0-9_.-]+[a-zA-Z0-9.-]$/
Try this
Description:
In the first section, [a-zA-Z0-9.-], regex only allows lower and upper case alphabets, digits, dot and hyphen.
In the next section, [a-zA-Z0-9_.-]+, regex looks for a single or more than one characters that are lower or upper case alphabets, digits dot, hyphen or an underscore.
The last part, [a-zA-Z0-9.-], is the same as the first part that restricts the input to end with an underscore.
Try this:
Recently had the same concern and this is how I did it.
// '"^[a-zA-Z0-9_.-]*$"' → Alphanumeric and 「.」「_」「-」
// "^[^_].*[^_]$" → Reject start and end of string if contains 「_」
// (?=) REGEX AND operator
SLUG_REGEX = '"(?=^[a-zA-Z0-9_.-]*$)(?=^[^_].*[^_]$)"';
I used this snippet for my Laravel Validation so you may need to change the code as needed like " to / based on your code sample and other answers' code.

regex validation - grails constraints

I'm pretty new on grails, I'm having a problem in matches validation using regex. What I wanted to happen is my field can accept a combination of alphanumeric and specific special characters like period (.), comma (,) and dash (-), it may accept numbers (099) or letters only (alpha) , but it won't accept input that only has special characters (".-,"). Is it possible to filter this kind of input using regex?
please help. Thank you for sharing your knowledge.
^[0-9a-zA-Z,.-]*?[0-9a-zA-Z]+?[0-9a-zA-Z,.-]*$
meaning:
/
^ beginning of the string
[...]*? 0 or more characters from this class (lazy matching)
[...]+? 1 or more characters from this class (lazy matching)
[...]* 0 or more characters from this class
$ end of the string
/
I think you could match that with a regular expression like this:
".*[0-9a-zA-Z.,-]+.*"
That means:
"." Begin with any character
"*" Have zero or more of these characters
"[0-9a-zA-Z.,-]" Have characters in the range 0-9, a-z, etc, or . or , or -
"+" Have one or more of this kind of character (so it's mandatory to have one in this set)
"." End with any character
"*" Have zero or more of these characters
This is working ok for me, hope it helps!

Resources