Regular expression not worknig - regex

I am trying to create a regular expression in javascript with the following rules:
At least 2 characters.
Should have at least 1 letter as a prefix and end with a . or have or - and then have more letters.
The following strings should be legal - aa, aaaaa, a., a-a, a a.
These should not be legal - a (too short), aa.aa. (two dots), aa- (after - should be another letter).
I don't know what I'm doing wrong here but my regex doesn't seem to work, as it is legal yet no word matches it:
(?=^.{2,}$)^(([a-z][A-Z])+([.]|[ -][a-zA-Z]+){0,1}$)

Had to re-write it completely to cover op's comment. The new regex would be:
^[a-zA-Z][a-zA-Z]*[ -][a-zA-Z]*[a-zA-Z]$|^[a-zA-Z][a-zA-Z]*([a-zA-Z]|\.)$
Explanation
1st Alternative ^[a-zA-Z][a-zA-Z]*[ -][a-zA-Z]*[a-zA-Z]$
^ asserts position at start of a line
[a-zA-Z] Match a single character present in [a-zA-Z]
[a-zA-Z]* * Quantifier — Matches between zero and unlimited
times(greedy)
[ -] Match a single character - or a space
$ asserts position at the end of a line
2nd Alternative
^[a-zA-Z][a-zA-Z]*([a-zA-Z]|\.)$
^ asserts position at start of a line
[a-zA-Z] Match a single character present in [a-zA-Z]
[a-zA-Z]* * Quantifier — Matches between zero and unlimited
times(greedy)
([a-zA-Z]|.) Match a single character present in the list below
[a-zA-Z] or dot
$ asserts position at the end of a line

Related

Regex for name type

I am working on regex with the following conditions:
Must contain from 1 to 63 alphanumeric characters or hyphens.
First character must be a letter.
Cannot end with a hyphen or contain two consecutive hyphens.
I am able to get the regex like:
^[a-zA-Z0-9](?!.*--)[a-zA-Z0-9-]{0,61}[A-Za-z0-9]$
But it fails on the length constraint as well as allows patterns like "a-". How can I meet the conditions?
I would phrase your requirements as:
^(?=.{1,63}$)(?!.*--)[a-zA-Z]([a-zA-Z0-9\-]*[a-zA-Z0-9])?$
Demo
Here is a brief explanation of what each part of the above regex does:
^ from the start of the match
(?=.{1,63}$) assert that the string is between 1 63 characters
(?!.*--) assert that two hyphens do not appear together anywhere
[a-zA-Z] first character is a letter (mandatory in all matches)
([a-zA-Z0-9\-]*[a-zA-Z0-9])?
The final portion says to match a final character which is alphanumeric, but not dash, possibly preceded by alphanumeric characters or dash.
My take on this would be:
^[A-Za-z](?!.*?--)[A-Za-z0-9\-]{0,62}(?<!-)$
Try it out here
Explanation:
^ - Matches the start of the string.
[A-Za-z] - Matches the first letter.
(?!.*?--) - Ensures that there are no two consecutive hyphens in the rest of the string.
[A-Za-z0-9\-]{0,62} - Matches the remaining alphanumeric and hyphen characters.
(?<!-) - Ensures that the string doesn't end with a hyphen.
$ - Matches the end of the string.

Working with regex for alphanumeric

I'm trying a regex fro Alpha Numeric of length 7 (with positions 1,3,4 as characters and positions 2,5,6,7 as digits).
[a-zA-Z]|[0-9]|[a-zA-Z]|[a-zA-Z]|[0-9]|[0-9]|[0-9]
Can someone help me?
The sequence "character, digit, character, character, digit, digit, digit" is expressed in regex as
[a-zA-Z][0-9][a-zA-Z]{2}[0-9]{3}
If you're working in PCRE (with say, PHP):
^([a-zA-Z])([0-9])(?1){2}(?2){3}$
Breakdown:
^ - from the start of the string
([a-zA-Z]) - match and capture a single character in the ranges given: a-z, A-Z
([0-9]) - match and capture a single character in the ranges given: 0-9
(?1){2} - redo the regex in the first group twice (recursive subpattern)
(?2){3} - redo the regex in the second group 3 times (recursive subpattern)
$ - the end of the string
If you want to match this in the middle of a sentence, exchange ^ and $ for \b - which will match a word boundary
See the demo
If you're not using PCRE:
^[a-zA-Z][0-9][a-zA-Z]{2}[0-9]{3}$
Which does the same thing, but has some copy-paste involved

regex nonconsecutive match

I'm trying to match a word that has 2 vowels in it (doesn't have to be consecutively) but the regex I've come up either matches nothing or not enough. This is the last iteration (dart).
final vowelRegex = new RegExp(r'[aeiouy]{2}');
Here's an example sentence being parsed and it should match, one, shoulder, their, and over. It's only matching shoulder and their. I understand why, because that's the expression I defined. How can the expression be defined to match on 2 vowels, regardless of position in the word?
one shoulder their the which over
The expression only needs to be tested on one word at a time so hopefully this simplifies things.
You can use :
new RegExp(r'(\w*[aeiouy]\w*){2}');
Both of the previous two answers are incorrect.
(\S*[aeiouy]\S*){2} can match substrings of non-whitespace characters even if they contain non-word characters (proof).
\S*[aeiouy]\S*[aeiouy]\S* has the same problem (proof).
Correct solution:
\b([^\Waeiou]*[aeiou]){2}\w*\b
And if you want only whitespace to count as the word boundary (rather than any non-word character), then use the following regex where the target word is in capture group \2.
(\s|^)(([^\Waeiou]*[aeiou]){2}\w*)(\s|$)
You can try this:
\S*[aeiouy]\S*[aeiouy]\S*
Explanation
\S* matches any non-whitespace character (equal to [^\r\n\t\f ])
* Quantifier — Matches between zero and unlimited times
[aeiou] Match a single character present in the list below [aeiou]
For input string : one shoulder their the which over
it will match four word: one shoulder their over
I'd do:
\b(?:\w*[aeiouy]+\w*){2,}\b
Explanation:
\b : word boundary
(?: : start non-capture group
\w* : 0 or more word characters
[aeiouy]+ : 1 or more vowels
\w* : 0 or more word characters
){2,} : end group repeated at least twice
\b : word boundary

Regular Expression Pattern After Each x characters

I need a Regex to match the following:
After each 6 characters of a string there is a ';'
Examples:
aaaaaa;z5z5z5;zdzzzt; (Valid)
aaadzdaaa;z5z5dzdzz5;zdzdzd; (Not Valid)
I'v tried:
(([A-Za-z0-9]{6};$))
but it only validates according to the last sequence.
You should use
^(?:[A-Za-z0-9]{6};)*$
See regex demo
If there must be at least one sequence with a semi-colon, replace * with + quantifier:
^(?:[A-Za-z0-9]{6};)+$
You actually need both ^ start-of-string anchor and $ end-of-string anchor, and you should not have placed the $ anchor into the repeated group since there is only one end of string.
Here is the regex breakdown:
^ - start of string
(?:[A-Za-z0-9]{6};)* - 0 or more sequences of...
[A-Za-z0-9]{6} - exactly 6 ASCII letters or digits
; - a semi-colon
$ - end of string.
I would use:
^(?:\w{6};)*$
With:
^ assert position at start of a line
(?:\w{6};)* Non-capturing group
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
\w{6} match any word character [a-zA-Z0-9_]
Quantifier: {6} Exactly 6 times
; matches the character ; literally
$ assert position at end of a line

Help with regular expression

In the following expression:
if (($$_ =~ /^.+:\s*\#\s*abcd\s+XYZ/)
Where is $$_ taken from?
The right side of the expression means to match one or more characters plus followed by colon, followed by zero or more spaces followed by # followed by one or more spaces folowed by 'abcd' followed by zero or more spaces followed by 'XYZ'?
You have the last "one or more" and "zero or more" reversed from what the regex actually does.
$$_ dereferences the scalar reference in $_.
Concerning 2., your explanation of the regex is not entirely correct.
/^.+:\s*#\s*abcd\s+XYZ/
means one or more characters (starting at the beginning of the string) followed by a colon, followed by zero or more whitespace characters, followed by one hash character, followed by zero or more whitespace characters, followed by 'abcd', followed by one or more whitespace characters, followed by 'XYZ'.
As for pt. 2:
Line beginning with (^) one or more characters (.+), colon (:), zero or more whitespace characters (\s*), a hash (\#), zero or more whitespace characters (\s*), the string "abcd" (abcd), one or more whitespace characters (\s+), then the string "XYZ" (XYZ).
(emphasis added on discrepancies.) Do note that there is no anchor on the end of line ($), thus this only concerns the beginning.
Have a look at this site
Here is the given explanation of your regex:
Token Meaning
^ Matches beginning of input. If the multiline flag is set to true,
also matches immediately after a line break character.
.+ Matches any single character except newline characters.
The + quantifier causes this item to be matched 1 or more times (greedy).
: :
\s* Matches a single white space character.
The * quantifier causes this item to be matched 0 or more times (greedy).
\# #
\s* Matches a single white space character.
The * quantifier causes this item to be matched 0 or more times (greedy).
abcd abcd
\s+ Matches a single white space character.
The + quantifier causes this item to be matched 1 or more times (greedy).
XYZ XYZ