I would like to have a regular expression that checks if string of up to 14 alpha-numeric chars. can include hyphen, not at the beginning or end.
This what I have so far:
var patt = new RegExp("^([a-zA-Z0-9]+(-[a-zA-Z0-9])*){1,14}$");
But it's not working - http://jsfiddle.net/u6cWs/1/
Any idea?
You need to use positive lookahead (count number of alpha-numeric chars with optional hyphen).
If only single hyphen is allowed:
^(?=([a-zA-Z0-9]-?){1,14}$)[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)?$
Demo
If multiple hyphens are allowed:
^(?=([a-zA-Z0-9]-?){1,14}$)[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$
Demo
Additional option:
^[a-zA-Z0-9](?:-?[a-zA-Z0-9]){0,13}$
Demo
Here is a simple solution that is faster because it does not use lookaheads:
^[A-Za-z0-9](?:[-A-Za-z0-9]{0,12}[A-Za-z0-9])?$
See demo.
How does it work?
Like your original pattern, this regex is anchored between ^ and $, enforcing our limit on the number of characters.
The first character has to be a letter or digit.
The rest of the string, included in a (?: non-capturing group, is made optional by the ? at the end. This rest of the string, if it is there (more than one character), must end with a letter or digit. In the middle, you can have between 0 and 12 letters, digits or hyphens.
Optionally
If you want your regex to be a little shorter, turn on the case-insensitive option, and remove either the lower-case chars or the upper-case ones, for instance:
^[a-z0-9](?:[-a-z0-9]{0,12}[a-z0-9])?$
Use two regexes for simplicity and readability.
First check that it matches this:
/^[A-Za-z0-9-]{1,14}$/
then check that it does NOT match this:
/^-|-$/
Related
I'm trying to come up with a regex for domain names that can either be 2-30 characters long with alphanumeric characters separated by a single hyphen with no other special characters allowed .
something like this thisi67satest-mydomain
What I have at the moment is this : /^[a-z0-9-]{2,30}$/ but this doesn't cover all scenarios especially with respect to the single hyphen.
I've always tried to google my way through these regexes. the above example will allow more than one hyphen which I don't want. How can i make the single hyphen mandatory?
Try this:
^(?=.{2,30}$)[a-z0-9]+-[a-z0-9]+$
^ the start of the line/string.
(?=.{2,30}$) ensures that the string between 2-30 characters.
[a-z0-9]+ one or more small letter or digit.
- one literal -.
[a-z0-9]+ one or more small letter or digit.
$ end of the line/string.
See regex demo
I think following pattern will work for you. Let me know if it work.
(\w|-(?!-)){2,30}
I have the regex as follow:
^[a-z|A-Z]((?!.*--).*[[:alnum:]]|[-]){1,22}[a-z|A-Z|0-9]$
For some reason, the length of the given string if set to 24+ is still accepted. The original capture group needs to be: string between 3-24 alphanumeric characters, must begin with a letter, end with a letter or digit, and cannot contain consecutive hyphens.
Why is the regex not checking the quantifier of length 1-22 in the middle part?
The main pattern should be just ^[a-z].{1,22}[a-z\d]$ to specify that the whole match must be 3-24 characters and have the required beginning and ending characters. You can use the case-insensitive modifier to make a-z match A-Z as well.
Then add a negative lookahead to prohibit .*--. The final result is:
^(?!.*--)[a-z].{1,22}[a-z\d]$
DEMO
Currently, I am not expert in Regex, but I tried below thing I want to improve it better, can some one please help me?
Pattern can contain ASCII letters, spaces, commas, periods, ', . and - special characters, and there can be one digit at the end of string.
So, it's working well
/^[a-z ,.'-]+(\d{1})?$/i
But I want to put condition that at least 2 letters should be there, could you please tell me, how to achieve this and explain me bit as well, please?
Note that {1} is always redundant in any regex, please remove it to make the regex pattern more readable. (\d{1})? is equal to \d? and matches an optional digit.
Taking into account the string must start with a letter, you can use
/^(?:[a-z][ ,.'-]*){2,}\d?$/i
Details:
^ - start of string
(?: - start of a non-capturing group (it is used here as a container for a pattern sequence to quantify):
[a-z] - an ASCII letter
[ ,.'-]* - zero or more spaces, commas, dots, single quotation marks or hyphens
){2,} - end of group, repeat two or more ({2,}) times
\d? - an optional digit
$ - end of string
i - case insensitive matching is ON.
See the regex demo.
The thing to change in your regex is + after the list of allowed characters.
+ means one or many occurrences of the provided characters. If you want to have 2 or more you can use {2,}
So your regex should look something like
/^[a-z ,.'-]{2,}\d?$/i
I want to replace anything other than character, spaces and number only in end with empty string or in other words: we replace any number or spaces comes in-starting or in-middle of the string replace with empty string.
Example
**Input** **Output**
Ndd12 Ndd12
12Ndd12 Ndd12
Ndd 12 Ndd 12
Nav G45up Nav Gup
Attempted Code
regexp_replace(df1[col_name]), "(^[A-Za-z]+[0-9 ])", ""))
You may use:
\d+(?!\d*$)|[^\w\n]+(?!([A-Z]|$))
RegEx Demo
Explanation:
\d+(?!\d*$): Match 1+ digits that are not followed by 0+ digits and end of line
|: OR
[^\w\n]+(?!([A-Z]|$)): Match 1+ non-word characters that are not followed by an uppercase letter or and end of line
if you use python, you can use regular expressions.
You can use the re module.
import re
new_string = re.sub(r"[^a-zA-Z0-9]","",s)
Where ^ means exclusion.
Regular expressions exist in other languages. So it would be helpful to find a regular expression.
I came up with this regex to capture all characters that you want to remove from the string.
^\d+|(?<=\w)\d+(?![\d\s])|(?<=\s)\s+
Do
regexp_replace(df1[col_name]), "^\d+|(?<=\w)\d+(?![\d\s])|(?<=\s)\s+", ""))
Regex Demo
Explanation:
^\d+ - captures all digits in a sequence from the start.
(?<=\w)\d+(?![\d\s]) - Positive look behind for a word character with a negative look ahead for a number followed by space and capturing a sequence of digits in the middle. (Captures digits in G45up)
(?<=\s)\s+ - positive look behind for a space followed by one or more spaces, capturing all additional spaces.
Note : This regex could be inefficient when matching large strings as it uses expensive look-arounds.
^\d+|(?<=\w)\d+(?![\d\s])|(?<=\s)\s+|(?<=\w)\W|\W(?=\w)|(?<!\w)\W|\W(?!\w)
I am trying to match Strings with optional number of hyphens.
For example,
string1-string2,
string1-string2-string3,
string1-string2-string3 and so on.
Right now, I have something which matches one hyphen. How can I make the regex to match optional number of hyphens?
My current regex is: arn:aws:iam::\d{12}:[a-zA-Z]/?[a-zA-Z]-?[a-zA-Z]*
What do I need to add?
Use this regex:
^\\w+(-\\w+)*$
Explanation:
\\w+ - match any string containing [a-zA-Z_0-9]
(-\\w+)* - match a hyphen followed by a string zero or more times
Regex101
Note that this won't match an empty string, or a string containing weird characters. You could handle these cases manually or you could update the regex.