Regex to allow groups of 7 numbers and spaces - regex

I'm looking for help with a regex.
My input field should allow only groups of up to 7 digits, and an unlimited number spaces whether at the beginning, middle, or end.
Here are a few examples of valid matches
Match1:
478 2635478 14587 9652
Match2 (spaces at the end):
14 2 55586
I tried this regex
^( )*[0-9]{1,7}(( )*[0-9]{1-7})*( )*$
It matches when the group is 8 digits.

Converting my comment to answer so that solution is easy to find for future visitors.
You may use this regex:
^ *[0-9]{1,7}(?: +[0-9]{1,7})* *$
RegEx Demo
RegEx Breakup:
^: Start
*: Match 0 or more spaces
[0-9]{1,7}: Match 1 to 7 digits
-(?: +[0-9]{1,7})*: Match 1+ spaces followed by a match of 1 to 7 digits. Repeat this group 0 or more times
*: Match 0 or more spaces
$: End

An idea with one group and use of a word boundary to separate blocks:
^ *(?:\d{1,7}\b *)+$
See this demo at regex101 (more explanation on the right side)
\b will require a space or the end after each \d{1,7} repetition.

Related

Regex to accept numbers with spaces

We have a expression to accept regex with spaces but the pattern should match below examples
ABC1234
TAC4 566
T A C 4 5 6 6
KA C4 56 6
Basically all spaces should be accepted with 3 alpha characters[A-z] and followed by 4 numbers[0-9]
I tried using this regex but it doesnt work :
^((\s)*([a-zA-Z]{3})([0-9]{4}))?$
Assuming no trailing/leading psaces (as per given sample data), the very verbose version could be:
^[A-Z](?: ?[A-Z]){2} ?\d(?: ?\d){3}$
See an online demo. It basically means the same as ^[A-Z] ?[A-Z] ?[A-Z] ?\d ?\d ?\d ?\d$ where:
^ - Match start-line anchor;
[A-Z] - An uppercase alpha;
(?: ?[A-Z]){2} - Non-capture group to match an optional space and an uppercase alpha twice;
?\d - Optional space and single digit;
(?: ?\d){3} - Non-capture group to match an optional space and a digit three times;
$ - End-line anchor.
Put \s* after the pattern for letter or number to allow any amount of spaces after that character. Put these in groups so you can then quantify them to allow 3 letters followed by 4 numbers.
^\s*([a-zA-Z]\s*){3}(\d\s*){4}$
/[A-Za-z]\s?[A-Za-z]\s?[A-Za-z]\s?\d\s?\d\s?\d\s?\d/g it's big and inelegant but it'll meet your criteria.
Regex101

Regex for first eight letters and last number

Please help me compose a working regular expression.
Conditions:
There can be a maximum of 9 characters (from 1 to 9).
The first eight characters can only be uppercase letters.
The last character can only be a digit.
Examples:
Do not match:
S3
FT5
FGTU7
ERTYUOP9
ERTGHYUKM
Correspond to:
E
ERT
RTYUKL
VBNDEFRW3
I tried using the following:
^[A-Z]{1,8}\d{0,1}$
but in this case, the FT5 example matches, although it shouldn't.
You may use an alternation based regex:
^(?:[A-Z]{1,8}|[A-Z]{8}\d)$
RegEx Demo
RegEx Details:
^: Start
(?:: Start non-capture group
[A-Z]{1,8}: Match 1 to 8 uppercase letters
|: OR
[A-Z]{8}\d: Match 8 uppercase letters followed by a digit
): End non-capture group
$: End
You might also rule out the first 7 uppercase chars followed by a digit using a negative lookhead:
^(?![A-Z]{1,7}\d)[A-Z]{1,8}\d?$
^ Start of string
(?![A-Z]{1,7}\d) Negative lookahead to assert not 1-7 uppercase chars and a digit
[A-Z]{1,8} Match 1-8 times an uppercase char
\d? Match an optional digit
$ End of string
Regex demo
With a regex engine that supports possessive quantifiers, you can write:
^[A-Z]{1,7}+(?:[A-Z]\d?)?$
demo
The letter in the optional group can only succeed when the quantifier in [A-Z]{1,7}+ reaches the maximum and when a letter remains. The letter in the group can only be the 8th character.
For the .net regex engine (that doesn't support possessive quantifiers) you can write this pattern using an atomic group:
^(?>[A-Z]{1,7})(?:[A-Z]\d?)?$

REGEX - how to match exacly 3 words from url?

I would like to match with regex exacly 3 words from search phrase from url but not match 4 or more. URL can have some variations. The problem is shown as below. Regex should match and not match following examples:
SHOULD MATCH:
https://example.com/search=any%20url%20encoded_word-here
https://example.com/search=any%20url%20encoded_word-here%20
https://example.com/search=z%C5%82oty%20z%C5%82oty%20z%C5%82oty
https://example.com/search=z%C5%82oty%20z%C5%82ota%20%C5%82ata
https://example.com/search=any%20%20word%20%20here
https://example.com/search=any%20word%20here&color=blue
https://example.com/search=any-1st%20word_2nd%20here3
SHOULD NOT MATCH:
https://example.com/search=one%20two%20three%20four
https://example.com/search=one%20%20two%20%20three%20%20four
https://example.com/search=one%20%20two%20three%20%20four
https://example.com/search=one%20%20two%20%20three%20%20four
https://example.com/search=one%20two%20three%20four&color=blue
https://example.com/search=z%C5%82oty%20z%C5%82oty%20z%C5%82oty%20word
Started here https://regex101.com/r/0qzCJV/1 but I have no idea how to not match on conditions. Can you pls help me guys?
You may use this regex with a negative lookahead to fail the match when there are 3 %20 followed by at least 1 more character:
^(?!(?:.+?%20){3}.)(?:.+?%20){2}.+?(?:%20)?$
RegEx Demo
RegEx Details:
^: Start
(?!(?:.+?%20){3}.): Negative lookahead to fail the match when we have 3 occurrences of %20 followed by at least 1 character
(?:.+?%20){2}: Match 1+ of any characters followed by %20. Repeat this match 2 times to match 2 words
.+?: Match 1+ of any characters
(?:%20)?: Match optional %20 before end
$: End
Or use possessive quantifier to reduce backtracking:
^(?!(?:.+?%20){3}+.)(?:.+?%20){2}.+?(?:%20)?$
Try this:
^(((?!%20).)*(%20)+){2}((?!%20).)*(%20)?$
See live demo.
This uses a negative look ahead to match up to %20, then any number of %20, and all that twice. Then finish with anything not %20, except there may be %20 at the end.
Note: Your examples non-matches did not include urls with less than 3, eg
https://example.com/search=one%20two

how to match a list of fixed length words separated by space or comma?

The words' length could be 2 or 6-10 and could be separated by space or comma. The word only include alphabet, not case sensitive.
Here is the groups of words that should be matched:
RE,re,rereRE
Not matching groups:
RE,rere,rel
RE,RERE
Here is the pattern that I have tried
((([a-zA-Z]{2})|([a-zA-Z]{6,10}))(,|\s+)?)
But unfortunately this pattern can match string like this: RE,RERE
Look like the word boundary has not been set.
You could match chars a-z either 2 or 6 - 10 times using an alternation
Then repeat that pattern 0+ times preceded by a comma or a space [ ,].
^(?:[A-Za-z]{6,10}|[A-Za-z]{2})(?:[, ](?:[A-Za-z]{6,10}|[A-Za-z]{2}))*$
Explanation
^ Start of string
(?:[A-Za-z]{6,10}|[A-Za-z]{2}) Match chars a-z 6 -10 or 2 times
(?: Non capturing group
[, ](?:[A-Za-z]{6,10}|[A-Za-z]{2}) Match comma or space and repeat previous pattern
)* Close non capturing group and repeat 0+ times
$ End of string
Regex demo
If lookarounds are supported, you might also assert what is directly on the left and on the right is not a non whitespace character \S.
(?<!\S)(?:[A-Za-z]{6,10}|[A-Za-z]{2})(?:[ ,](?:[A-Za-z]{6,10}|[A-Za-z]{2}))*(?!\S)
Regex demo
([a-zA-Z]{2}(,|\s)|[a-zA-Z]{6,10}|(,|\s))
This one will get only the words who have 2 letter, or between 6 and 10
\b,?([a-zA-Z]{6,10}|[a-zA-Z]{2}),?\b
You can use this
^(?!.*\b[a-z]{4}\b)(?:(?:[a-z]{2}|[a-z]{6,10})(?:,|[ ]+)?)+$
Regex Demo
This regex will match your first case, but neither of your two other cases:
^((([a-zA-Z]{2})|([a-zA-Z]{6,10}))(,|[ ]+|$))+$
I'm making the assumption here that each line should be a single match.
Here it is in action.

regex how to match mutiple pattern

what pattern should i use in regex if i want to match the first pattern but then i want to unmatch the second pattern.
for example i want to match the string 'id' followed by decimal as long as that decimal is not 6 or 9.
so it should match id1,id2,id3 ... etc but not id6 and id9.
I tried this pattern and it's not working :
"id(\d|(?!6|9))"
You can use negative lookahead like this.
Regex: \bid(?![69])\d\b
Explanation:
\b ensures the word boundary.
(?![69]) negative lookahead makes sure that number is not 6 or 9.
\d matches a single digit after id.
Regex101 Demo
Its not the best solution but you can also do this using positive look ahead as
\bid(?=\d)(?:\d\d+|[^69])\b
Regex Breakdown
\b #word boundary
id #Match id literally
(?=\d) #Find if the next position contains digit (otherwise fails)
(?: #Non capturing group
\d\d+ #If there are more than one digits then match is success
| #OR (alternation)
[^69] #If its single digit don't match 6 or 9
) #End of non capturing group
\b
Regex Demo
If you want to check id is not followed by 6 or 9 and you want to accept cases like id16 but not id61, then you can use
\bid(?=\d)[^69]\d*\b
Regex Demo
The id(\d|(?!6|9)) pattern matches id followed with any 1 digit or if there is no 6 or 9. That alternation (\d or (?!6|9)) allows id6 and id9 because the first alternative "wins" in NFA regex (i.e. the further alternatives after one matches are not tested against).
If you need to only exclude id matches with 6 or 9 use
\bid(?![69]\b)\d+\b
See the regex demo
If you want to avoid matching all id with 6 and 9 following it, use
\bid(?![69])\d+
See another regex demo.
Here, \d+ matches one or more digits, \b stands for a word boundary (the digits should be preceded and followed with non-"word" characters), and the (?![69]) lookahead fails the match if there is 6 or 9 after id (with or without a word boundary check - depending on what you need).
UPDATE
If you need to exclude the id whose number does not start with 6 or 9, you can use
\bid[0-578]\d*
(demo)
Based on Shafizadeh's comment.