I've got a RegEx pattern fro user first and last name. They can only contain alphabetic characters, spaces (0 - any ), hyphens (0 - any), apostrophes ' (0 - any ) and number of symbols 1 - 40. First and Last name can not start with ' or whitespace
Here is my pattern code.
^^[a-zA-Z]+[\-\'\s]?[a-zA-Z ]{1,40}$
But RegEx does now allow to use this sample Endevald O'McKnight
You may use
^(?=.{1,40}$)[a-zA-Z]+(?:[-'\s][a-zA-Z]+)*$
See the regex demo.
Details
^ - a start of a string
(?=.{1,40}$) - there must be 1 to 40 chars other than line break chars in the string
[a-zA-Z]+ - 1 or more ASCII letters
(?: - starto of a non-capturing group repeated 0 or more times matching sequences of
[-'\s] - a -, ' or whitespace
[a-zA-Z]+ - 1+ ASCII letters
)* - end of the grouping
$ - end of string
Related
I need to take only a number (a float number) from a text, but I can't remove the whitespaces...
** Update
I have a problem with this method, I only need to consider numbers and ',' between '- EUR' and 'Fee' as rule.
You can use
- EUR\W*(.*?)\W*Fee
See the regex demo.
Variations of the regex that might work in different regex engines:
- EUR\W*\K.*?(?=\W*Fee)
(?<=- EUR\W*).*?(?=\W*Fee)
Details:
- EUR - literal text
\W* - zero or more non-word chars
(.*?) - Group 1: any zero or more chars other than line break chars as few as possible
\W*- zero or more non-word chars
Fee - a string.
You could also match the number format in capture group 1
- EUR\b\D*(\d+(?:,\d+)?)\s+Fee\b
- EUR\b Match - EUR and a word boundary
\D* Match 0+ times any char except a digit
( Capture group 1
\d+(?:,\d+)? Match 1+ digits with an optional decimal part
) Close group 1
\s+Fee\b Match 1+ whitespace chars, Fee and a word boundary
Regex demo
this is working i removed the , from (.) in test string.
Regex example - working
I'm trying to build a regex where it accepts domain names with the following conditions:
Allows DNS names (only hyphens, periods and alphanumeric characters allowed) upto 255 characters.
Hyphens can only appear in between letters
Should start with a letter and end with a letter. It will have minimum 3 characters (letters and periods mandatory, hyphen is optional.)
The length of the label before a period should be 63
Possible Cases:
a.b.c
a-a.b
Cases that should not pass
a-.b
qwertqwertqwertqwertqwertqwertqwertqwertqwertqwertqwertqwertqwerhhg.v
aaaa
aaa-a
What I have built looks like this:
^(([a-zA-z0-9][A-Z0-9a-z-]{1,61}[a-zA-Z0-9][.])+[a-zA-Z0-9]+)$
But this does not accept a.b.c
You may use
^(?=.{1,255}$)(?=[^.]{1,63}(?![^.]))[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*(?:[.](?=[^.]{1,63}(?![^.]))[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*)+(?:[.][a-zA-Z0-9-]*[a-zA-Z0-9])?$
See the regex demo here.
Pattern details
^ - start of string
(?=.{1,255}$) - the whole string should have 1 to 255 chars
(?=[^.]{1,63}(?![^.])) - there must be 1 to 63 chars other than . before the char other than . or end of string
[a-zA-Z0-9]+ - 1 or more alphanumeric chars
(?: - start of a non-capturing group:
- - a hyphen
[a-zA-Z0-9]+ - 1+ alphanumeric chars
)* - zero or more repetitions
(?: - start of a non-capturing group...
[.] - a dot
(?=[^.]{1,63}(?![^.])) - there must be 1 to 63 chars other than . before the char other than . or end of string
[a-zA-Z0-9]+ - 1+ alphanumeric chars
(?:-[a-zA-Z0-9]+)* - 0 or more repetitions of a - followed with 1+ alphanumeric chars
)+ -... 1 or more times
(?: - start of a non-capturing group...
[.] - a dot
[a-zA-Z0-9-]* - 1+ alphanumeric or - chars
[a-zA-Z0-9] - an alphanumeric char (no hyphens at the end)
)? -... 1 or 0 times (it is optional)
$ - end of string.
You can use the following regex:
/^(?=[A-Z])((?:[A-Z\d]|(?<=[A-Z])-(?=[A-Z])){1,63})(?<=[A-Z])(?:\.[A-Z\d]+){1,2}$/im
Details:
^ - Start of the string.
(?=[A-Z]) - Positive lookahead: The whole string must start with a letter.
( - A capturing group - the domain name.
(?: - Start of a non-capturing group, needed due to the following quantifier.
[A-Z\d] - The first alternative: Either a letter or a digit.
| - Or.
(?<=[A-Z])-(?=[A-Z]) - The second alternative: A hyphen, preceded with a letter
and followed with a letter.
) - End of the non-capturing group.
{1,63} - This group (either alternative) must occur up to 63 times.
) - End of the capturing group.
(?<=[A-Z]) - Positive lookbehid: The capturing group just matched (domain name)
must end with a letter.
(?: - A non-capturing group, also needed due to the following quantifier.
\.[A-Z\d]+ - A dot and a sequence of letters or digits.
) - End of the non-capturing group.
{1,2} - This group must occur 1 or 2 times.
$ - End of the string.
You should definitely use i (case insensitive) option and if you check
a number of strings, each in a separate row, also m (multiline) option.
I didn't include any test for the whole length, but you didn't include it either.
I think, the main task here was to show how to match the case your regex failed.
I was using the below pattern.
/^[A-Za-z0-9]+(-[A-Za-z0-9]+)*$/.
What i need is that it should not be allowing the hyphen between 2 numbers.
I know that we have to make modification with 0-9, where we can restrict user from entering them twice.
The (?!.*[0-9]-[0-9]) lookahead after ^ will make sure there is no digit-digit pattern in the string. Also, if there must be 1 or 0 hyphens, replace * at the end with ? (0 or more occurrences).
Use
^(?!.*[0-9]-[0-9])[A-Za-z0-9]+(-[A-Za-z0-9]+)?$
See the regex demo.
Details
^ - start of string
(?!.*[0-9]-[0-9]) - a negative lookahead that fails the match if, after any 0+ chars other than line break chars, there is a digit, hyphen, digit pattern
[A-Za-z0-9]+ - 1 or more ASCII alphanumeric chars
(-[A-Za-z0-9]+)? - 1 or 0 sequences of:
- - a hyphen
[A-Za-z0-9]+ - 1 or more ASCII alphanumeric chars
$ - end of string.
I have an identifier that contains letters or digits and dashes.
What I would like to do is to keep the first 3 letters before the first dash and delete the rest and then keep the 2 first letters after the first dash.
For instance, I have the following id :
9D3236A9-B496-4597-87E4-3A3FB69D07BF
The output ID should be : 9D3B445873A3.
I have tried:
^.{3}\-
but nothing happens. Can you please help with that?
You may use
^([A-Za-z0-9]{3})[A-Za-z0-9]*|-([A-Za-z0-9]{3})[A-Za-z0-9]*$|-([A-Za-z0-9]{2})[A-Za-z0-9]*
Replace with $1$2$3. See the regex demo.
Details
^ - start of string
([A-Za-z0-9]{3}) - Group 1 ($1 in the replacement): 3 alphanumeric chars
[A-Za-z0-9]* - 0+ alphanumerics
| - or
- - a hyphen
([A-Za-z0-9]{3}) - Group 2 ($2 in the replacement): 3 alphanumeric chars
[A-Za-z0-9]* - 0+ alphanumerics
$ - end of string
|
- - a hyphen
([A-Za-z0-9]{2}) - Group 3 ($3 in the replacement): 2 alphanumeric chars
[A-Za-z0-9]* - 0+ alphanumerics.
You can use the regex given in this demo
(^.{3})[a-z0-9A-Z]*((?>-).{2})[a-z0-9A-Z]*((?>-).{2})[a-z0-9A-Z]*((?>-).{2})[a-z0-9A-Z]*((?>-).{2})[a-zA-Z0-9]*
I have following regex;
^(\s)*[+-]?\d+$
It fails if input contains multiple whitespaces before first non-whitespace character.
Currently it is working on next examples
- :false
-1 :true
+1 :true
What I want is same logic if there is 0,1 or more whitespaces at the beginning:
: true (empty input string)
: true (one or more spaces)
-: false
-1: true
+1: true
235: true
Here I'm matching numbers, but on more general scheme I would like same behaviour if there are decimalan, on some special words etc.
So, basicly, I want that my regex match if there is any number of whitespaces at the beginning or empty string, followed by something I wannna match (number, email, special words...)
You need to make the whole pattern optional with an optional grouping construct and put the \s* before the group:
^\s*(?:[+-]?\d+)?$
^^^ ^^
See the regex demo
Details:
^ - start of a string
\s* - 0+ whitespaces
(?: - start of a non-capturing group (if the engine does not support non-capturing groups, remove ?:) matching....
[+-]? - an optional (1 or 0 occurrences) + or - symbols
\d+ - 1+ digits
)? - .... 1 or 0 times
$ - end of string.
I think you want the asterisk in with the \s:
^\s*[-+]?\d+$