Regex to accept numbers with spaces - regex

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

Related

Regex to allow groups of 7 numbers and spaces

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.

Regex: Replace certain part of the matched characters

I want to be able to match with a certain condition, and keep certain parts of it. For example:
JuneĀ 2021 9 Feature Article Three-Suiters Via Puppets Kai-Ching Lin
should turn into:
JunĀ 2021 Three-Suiters Via Puppets Kai-Ching Lin
So, everything until the end of the word Article should be matched; then, only the first three characters of the months is kept, as well as the year, and this part is going to replace the matched characters.
My strong regex knowledge got me as far as:
.+Article(?)
You could use 2 capture groups and use those in a replacement:
\b([A-Z][a-z]+)[a-z](\s+\d{4})\b.*?\bArticle\b
\b A word boundary to prevent a partial word match
([A-Z][a-z]+) Capture group 1, match a single uppercase char and 1+ lowercase chars
[a-z] Match a single char a-z
(\s+\d{4})\b Capture group 2, match 1+ whitspace chars and 4 digits followed by a word boundary
.*?\bArticle\b Match as least as possible chars until Article
Regex demo
The replaced value will be
Jun 2021 Three-Suiters Via Puppets Kai-Ching Lin
You could use positive lookbehinds:
(?<=^[A-Z][a-z]{2})[a-z]*|(?<=\d{4}).*Article
(?<=^[A-Z][a-z]{2}) - behind me is the start of a line and 3 chars; presumably the first three chars of the month
[a-z]* - optionally, capture the rest of the month
| - or
(?<=\d{4}) - behind me is 4 digits; presumably a year
.*Article - capture everything leading up to and including "Article"
https://regex101.com/r/fbYdpH/1

Regular Expression positive lookbehind

I would like to create a regex, that allowes the following patterns:
1234
1234567
123456789
12345678900-
12345678900-123456
It should be possible to only insert numbers and only one hyphen is allowed.
I tried with the following regex:
^[0-9]{1,11}(?(?<=\d{11})[-]?|)[0-9]{6}
It should not be possible to have 11 characters without the hyphen at the end(12345678900 is wrong).
Unfortunatly it didnt work as I intended.
You can match 1-10 digit and optionally match 1 digit followed by - and 6 digits.
^\d{1,10}(?:\d?-(?:\d{6})?)?$
^ Start of string
\d{1,10} Match 1-10 digits
(?: Non capture group
\d?- Match a single optional digit and -
(?:\d{6})? Match optional 6 digits
)? Close non capture group and make it optional
$ End of string
Regex demo
Another variation could be matching 1-10 digits or match 11 digits with a hyphen and optionally 6 digits if the hyphen should only possible after 11 digits.
^(?:\d{1,10}|\d{11}-(?:\d{6})?)$
Regex demo
You can use
^[0-9]{1,11}(?:(?<=[0-9]{11})-(?:[0-9]{6})?)?$
^\d{1,11}(?:(?<=\d{11})-(?:\d{6})?)?$
See the regex demo. Using \d is possible in case it matches ASCII digits in your regex flavor or you do not care if \d matches all possible Unicode digit chars or not.
Details:
^ - start of string
[0-9]{1,11} - one to eleven digits
(?:(?<=[0-9]{11})-(?:[0-9]{6})?)? - an optional occurrence of
(?<=[0-9]{11}) - immediately to the left there must be 11 digits
- - a hyphen
(?:[0-9]{6})? - an optional occurrence of six digits
$ - end of string.

Regex match checksum with or without dashes

To match a dash-less checksum I can do something like:
\b[0-9a-z]{32}\b
However, I'm seeing some checksums that also have dashes, such as:
d3bd55bf-062f-473b-9417-935f62c4c98a
While this is probably a fixed size, 8, then 4, then 4, then 4, then 12, I was wondering if I could do a regex where the number of non-dash digits adds up to 32. I think the answer is no, but hopefully some regex wizard can come up with something.
Here is a starting point for some sample inputs: https://regex101.com/r/K0IMKe/1.
You can use
\b[0-9a-z](?:-?[0-9a-z]){31}\b
See the regex demo.
It matches
\b - a word boundary
[0-9a-z] - a digit or a lowercase ASCII letter
(?:-?[0-9a-z]){31} - thirty-one repetitions of an optional - followed with a single digit or a lowercase ASCII letter
\b - a word boundary.
If you do not mind having a trailing - if there is a word char after it, at the end of a match, you may also use
\b(?:[0-9a-z]-?){32}\b
See this regex demo. Here, (?:[0-9a-z]-?){32} will match thirty-two repetitions of a digit or lowercase ASCII letter followed with an optional hyphen.
If there can be multiple dashes, you can assert 32 to 36 chars using a positive lookahead.
^(?=[a-z0-9-]{32,36}$)[a-z0-9]+(?:-[a-z0-9]+)*$
^ Start of string
(?=[a-z0-9-]{32,36}$) Positive lookahead, assert what is at the right is 32 - 36 repetitions of the listed characters
[a-z0-9]+ Match 1+ times any of the listed
(?: Non capture group
-[a-z0-9]+ Match a - followed by 1+ times any of the listed (the string can not end with a hyphen)
)* Close the group and match 0+ times to also match the string without dashes
$ End of string
Regex demo
If you want to limit the amount of dashes to 0 -4 times, you can change the quantifier * to {0,4}+
^(?=[a-z0-9-]{32,36}$)[a-z0-9]+(?:-[a-z0-9]+){0,4}+$
Regex demo

How can I allow one space in a regular expression

The following Regex checks for a number which starts with 6, 8 or 9 and has to be exactly 8 digits long.
/^(6|8|9)\d{7}$/
Now I want to accept one space in between digits as well, but don't know where to start.
For example both 61234567 and 6123 4567 should be allowed, but only the first one passes my current regex.
Can you help me create it?
You may use
^(?!.*(?:\s\d+){2})[689](?:\s?\d){7}$
See the regex demo
Details
^ - start of string
(?!.*(?:\s\d+){2}) - a negative lookahead that fails the match if, after any 0+ chars other than line break chars, as many as possible occurrences, there are two occurrences of a whitespaces followed with 1+ digits
[689] - 6, 7 or 9
(?:\s?\d){7} - seven occurrences of an optional whitespace followed with a single digit
$ - end of string.
To allow leadign/trailing whitespace, add \s? (1 or 0) or \s* (0 or more) right after ^ and before $.
To allow a single 1+ more whitespace chunk in the digit string, use
^(?!.*(?:\s+\d+){2})[689](?:\s*\d){7}$
See this regex demo.
You could use the regular expression
/^[689](?:\d{7}|(?=.{8}$)\d* \d+)$/
demo
We can make this self-documenting by writing it in free-spacing mode:
/
^ # match beginning of line
[689] # match '6', '8' or '9'
(?: # begin non-capture group
\d{7} # match 7 digits
| # or
(?=.{8}$) # require the remainder of the line to have 8 chars
\d*\ \d+ # match 0+ digits, a space, 1+ digits
) # end non-capture group
$ # match end of line
/x # free-spacing regex definition mode