Regex string doesn't contain 2 dots in a row - regex

I'd like to know if this regex expression is correct for checking that a string doesn't start with a dot, doesn't end with a dot and contains at least one dot anywhere but not the start or end:
My issue is that I can't figure on how to check if there's 2 dots in a row.
/^([^.])+([.])+.*([^.])$/

It seems you need to use
^[^.]+(?:\.[^.]+)+$
See the regex demo
Details:
^ - start of string
[^.]+ - 1+ chars other than a . (so, the first char cannot be .)
(?:\.[^.]+)+ - 1 or more (thus, the dot inside a string is obligatory to appear at least once) sequences of:
\. - a dot
[^.]+ - 1+ chars other than . (the + quantifier makes a char other than . appear at least once after a dot, thus, making it impossible to match the string with 2 dots on end)
$ - end of string.

You're close, have a try with:
^[^.]+(?:\.[^.]+){2,}$
It maches strings that have 2 or more dot, but not at the begining or at the end.
If you want one or more dot:
^[^.]+(?:\.[^.]+)+$
If you want one or two dots:
^[^.]+(?:\.[^.]+){1,2}$

Related

RegExp - find 1,2,3,6,7,8 and 9th letter from the end of the string

I'm new to regular expressions and trying to figure out which expression would match 1,2,3 and 6,7,8,9th letter in the string, starting from the end of the string. It would also need to include \D (for non-digits), so if 3rd letter from the end is a number it will exclude it.
Example of a string is
Wsd-kaf_23psd_trees32rap
So the result should be:
reesrap
or for
Wsd-kaf_23psd_trees324ap
it would be
reesap
This
(?<=^.{9}).*
gives me last 9 chars, but that's not really what I want.
Does anyone knows how can I do that?
Thanks.
You could try to use alternations to find all characters upto the position that holds 9 character untill the end or consecutive digits:
(?:^.*(?=.{9})|\d+)
See an online demo. Replace with empty string.
(?: - Open non-capture group;
^.* - Any 0+ characters (greedy), upto;
(?=.{9}) - A positive lookahead to assert position is followed by 9 characters;
| - Or;
\d+ - 1+ digits.
If, however, your intention was to match the characters seperately, then try:
\D(?=.{0,8}$)
See an online demo. Any non-digit that has 0-8 characters upto the end-line character.

RegEx: How to match a whole string with fixed-length region with negative look ahead conditions that are overriden afterwards?

The strings I parse with a regular expression contain a region of fixed length N where there can either be numbers or dashes. However, if a dash occurs, only dashes are allowed to follow for the rest of the region. After this region, numbers, dashes, and letters are allowed to occur.
Examples (N=5, starting at the beginning):
12345ABC
12345123
1234-1
1234--1
1----1AB
How can I correctly match this? I currently am stuck at something like (?:\d|-(?!\d)){5}[A-Z0-9\-]+ (for N=5), but I cannot make numbers work directly following my region if a dash is present, as the negative look ahead blocks the match.
Update
Strings that should not be matched (N=5)
1-2-3-A
----1AB
--1--1A
You could assert that the first 5 characters are either digits or - and make sure that there is no - before a digit in the first 5 chars.
^(?![\d-]{0,3}-\d)(?=[\d-]{5})[A-Z\d-]+$
^ Start of string
(?![\d-]{0,3}-\d) Make sure that in the first 5 chars there is no - before a digit
(?=[\d-]{5}) Assert at least 5 digits or -
[A-Z\d-]+ Match 1+ times any of the listed characters
$ End of string
Regex demo
If atomic groups are available:
^(?=[\d-]{5})(?>\d+-*|-{5})[A-Z\d_]*$
^ Start of string
(?=[\d-]{5}) Assert at least 5 chars - or digit
(?> Atomic group
\d+-* Match 1+ digits and optional -
| or
-{5} match 5 times -
) Close atomic group
[A-Z\d_]* Match optional chars A-Z digit or _
$ End of string
Regex demo
Use a non-word-boundary assertion \B:
^[-\d](?:-|\B\d){4}[A-Z\d-]*$
A non word-boundary succeeds at a position between two word characters (from \w ie [A-Za-z0-9_]) or two non-word characters (from \W ie [^A-Za-z0-9_]). (and also between a non-word character and the limit of the string)
With it, each \B\d always follows a digit. (and can't follow a dash)
demo
Other way (if lookbehinds are allowed):
^\d*-*(?<=^.{5})[A-Z\d-]*$
demo

Regex match string 3-6 characters long, at least one letter, no duplicate "-"

I have to match a string that is 3-6 characters long, contains at least one letter, but can have letters, numbers and only 1 "-".
The "-" must not be at the start or at the beginning.
Match:
string
str-ng
st-ng
s1-1g
st-1g
Do not match:
strings
-string
string-
st--ng
s-tn-g
1111
st
The closest I've gotten is this:
^((?!-.*-)[0-9A-Z]{3,6})$
But this divides the regex match with - So it matches s-tri but not st-ri because there aren't 3 chars at each end
Maybe you can use:
^(?=.*[a-z])(?!-|.*-$|.*-.*-)[a-z\d-]{3,6}$
See the online demo
^ - Start string anchor.
(?=.*[a-z]) - Positive lookahead to make sure there is at least one letter.
(?!-|.*-$|.*-.*-) - Negative lookahead to prevent a hyphen at the beginning or at the end or multiple.
[a-z\d-]{3,6} - Three to six times a character from the give class.
$ - End string anchor.
Note that I used the case-insensitive flag.
You can use
^(?=.{3,6}$)(?=[^a-zA-Z]*[A-Za-z])[0-9a-zA-Z]+(?:-[0-9a-zA-Z]+)?$
See the regex demo. Details:
^ - start of string
(?=.{3,6}$) - string must contain three to six chars other than line break chars
(?=[^a-zA-Z]*[A-Za-z]) - there must be at least one ASCII letter in the string
[0-9a-zA-Z]+ - one or more alphanumeric ASCII chars
(?:-[0-9a-zA-Z]+)? - an optional sequence of - and then one or more alphanumeric ASCII chars
$ - end of string.
Looking at the pattern that you tried, you meant to exclude the match when there are 2 hyphens present using the negative lookahead.
Also this part [0-9A-Z]{3,6} does not match a hyphen.
Reading
The "-" must not be at the start or at the beginning.
You might do that using
^(?![^\n-]*-[^\n-]*-)(?=[^a-zA-Z\n]*[a-zA-Z])[a-zA-Z0-9][a-zA-Z0-9-]{2,5}$
Regex demo
If you meant also no - at the end:
^(?![^\n-]*-[^\n-]*-)(?=[^a-zA-Z\n]*[a-zA-Z])[a-zA-Z0-9][a-zA-Z0-9-]{1,4}[a-zA-Z0-9]$
Explanation
^ Start of string
(?![^\n-]*-[^\n-]*-) Assert not 2 times -
(?=[^a-zA-Z\n]*[a-zA-Z]) Assert a char a-zA-Z
[a-zA-Z0-9] Match One of the listed without -
[a-zA-Z0-9-]{1,4} Repeat 1-4 times any of the listed including -
[a-zA-Z0-9] Match One of the listed without -
$ End of string
Regex demo

Regex match exactly 1 anywhere in string

So I need to match upper and lower case a-z letters, period (.) and # in a string. As a complication the string must have # exactly once anywhere in the string and . at least once anywhere in the string.
abcd#. // match
#ab.cd // match
a#cd#. // no match
abcd# // no match
I've tried to be clever (obviously not very) by doing look ahead but this one seems tricky eg.
(?=[#]){1}[a-zA-Z#]+$
The (?=[#]){1}[a-zA-Z#]+$ pattern matches any substring that starts with # and then has zero or more letters or # up to the end of the string. Look at what it matches.
You need to use
^(?=[^#]*#[^#]*$)(?=[^.]*\.)[a-zA-Z#.]+$
Or, if there must be also one dot (and no more than one) in the string
^(?=[^#]*#[^#]*$)(?=[^.]*\.[^.]*$)[a-zA-Z#.]+$
See the regex demo #1 and the regex demo #2.
Details
^ - start of string
(?=[^#]*#[^#]*$) - requires only one # and no more than one in string - a positive lookahead that requires 0+ chars other than #, a #, and again zero or more chars other than # till the end of string
(?=[^.]*\.) - requires at least one dot - a positive lookahead that requires 0+ chars other than . and then a .
(?=[^.]*\.[^.]*$) - requires only one dot and no more than one in string - a positive lookahead that requires 0+ chars other than ., a ., and again zero or more chars other than . till the end of string
[a-zA-Z#.]+ - one or more ASCII letters, # or .
$ - end of string.
Another option could be using a single lookahead asserting # and match a dot between 2 character classes, or the other way around asserting a dot and matching #
^(?=[^#]*#[^#]*$)[A-Za-z#]*\.[A-Za-z#]*$
Explanation
^ Start of string
(?=[^#]*#[^#]*$) Assert only 1 # char in the string
[A-Za-z#]*\.[A-Za-z#]* Match a dot between optionally repeating character classes each matching 1 out of A-Za-z#
$ End of string
Regex demo
For and . at least once anywhere in the string , you can allow matching a dot in the second character class:
^(?=[^#]*#[^#]*$)[A-Za-z#]*\.[A-Za-z#.]*$
Regex demo
I'm thinking you could just use:
^(?=.*\.)[a-zA-Z.]*#[a-zA-Z.]*$
See the online demo.
^ - Start string ancor.
(?=.*.) - Positive lookahead for any amount of characters up to a literal dot.
[a-zA-Z.]* - Zero or more characters from upper/lowercase letters or a dot.
# - A single #.
[a-zA-Z.]* - Zero or more characters from upper/lowercase letters or a dot.
$ - End string ancor.

Regex : Match everything after first dash

I have a string which contains the rego number of the car like
1FX9JE - 2012 Audi A3 Ambition Sportback MY12 Stronic
I would like to match everything except the rego number, so anything after the dash.
The regex I came up with is (php)
\s.[^-]*$
My initial regex which i came up can match anything after the dash only if the string contains only 1 dash. For example https://regex101.com/r/Jao8W0/1
However, if the string has more than 1 dash. The regex is not usable.
For example : https://regex101.com/r/Jao8W0/2
Is there anyway for me to match anything after the first dash even though the string contains additional dash after the first dash.
Thank you
Try this Regex:
^[^-\r\n]+-\s*\K.*$
Click for Demo
Explanation:
^ - asserts the start of the string
[^-\r\n]+ - matches 1+ occurrences of any character that is neither a - or nor a newline
-\s* - matches the first - in the string followed by 0+ whitespaces
\K - forgets everything matched so far
.* - matches 0+ occurrences of any character
$ - asserts the end of the string
if only has one space, you can use this pattern:
(?<=\-\s)(.*)
else if there may have more than one space, get the group(1) from match
(?<=\-)\s*(.*)
(?<=...) Ensures that the given pattern will match, ending at the
current position in the expression. The pattern must have a fixed
width. Does not consume any characters.