Comma Separated Numbers Regex - regex

I am trying to validate a comma separated list for numbers 1-8.
i.e. 2,4,6,8,1 is valid input.
I tried [0-8,]* but it seems to accept 1234 as valid. It is not requiring a comma and it is letting me type in a number larger than 8. I am not sure why.

[0-8,]* will match zero or more consecutive instances of 0 through 8 or ,, anywhere in your string. You want something more like this:
^[1-8](,[1-8])*$
^ matches the start of the string, and $ matches the end, ensuring that you're examining the entire string. It will match a single digit, plus zero or more instances of a comma followed by a digit after it.

/^\d+(,\d+)*$/
for at least one digit, otherwise you will accept 1,,,,,4

[0-9]+(,[0-9]+)+
This works better for me for comma separated numbers in general, like: 1,234,933

You can try with this Regex:
^[1-8](,[1-8])+$

If you are using python and looking to find out all possible matching strings like
XX,XX,XXX or X,XX,XXX
or 12,000, 1,20,000 using regex
string = "I spent 1,20,000 on new project "
re.findall(r'(\b[1-8]*(,[0-9]*[0-9])+\b)', string, re.IGNORECASE)
Result will be ---> [('1,20,000', ',000')]

You need a number + comma combination that can repeat:
^[1-8](,[1-8])*$
If you don't want remembering parentheses add ?: to the parens, like so:
^[1-8](?:,[1-8])*$

Related

Return dash followed by a single character

This works as expected:
([^\u0000-\u007F])+-हा([^\u0000-\u007F])+
Returns:
ब-हाणपूर
ब-हाणी
बनियन-हाफ
But I am looking for 1 character followed by dash. The expected output is:
ब-हाणपूर
ब-हाणी
I tried to replace + sign with character count like this...
([^\u0000-\u007F]){1}-हा([^\u0000-\u007F])+
But it returned the same 3 results. How do I return the first 2?
You need anchors:
^([^\u0000-\u007F])-हा([^\u0000-\u007F])+$
Demo
You asked 'What if I need 5 characters to the left of dash?'
The regex portion [^\u0000-\u007F] as written matches a single character that meets that criterion. If you want more or less than one, use a regex quantifier to describe how many you want.
In this case, if you want 5, you would use:
^([^\u0000-\u007F]{5})-हा([^\u0000-\u007F])+$
Probably like this:
^([^\u0000-\u007F]){1}-हा([^\u0000-\u007F])+
^([^\u0000-\u007F]{1})-हा([^\u0000-\u007F]+)
(\b[^\u0000-\u007F]{1})-हा([^\u0000-\u007F]+)
Regex demo

Regex - Match n last numbers without the last one

With regex - replace, I am trying to format a number like this:
The leading number should be separated by a +. Moreover, the last number should be separated by a + as well. The more tricky part is, that adjacent 1s to the + to the middle part should be removed, without touching the first and the last number, e.g.,
011023040 -> 0+02304+0
111023920443 -> 1+02392044+3
13242311 -> 1+32423+1
I almost achieved this with the following regex:
'^([0-9]{1})([1]+)?([0-9*)(0-9]{1}$'
And replace this with
'\1+\3+\4'
However, I have a problem with the last example, as this returns:
1+324231+1
However, the one before the second + should be removed.
Can anyone help me with this problem?
You have to use a non-greedy quantifier:
^([0-9])1*([0-9]*?)1*([0-9])$
^^
Live demo
I managed to group the numbers in the following way
^(\d)(1*)(\d+)(\d)$
by using multiline and global flags.
The replacement should look like \1+\3+\4

Regular Expression for 8 or more A's followed by zero, one or two equal signs (=)

I need to filter out garbage string values, which come in the form of at least 8 A's sometimes followed by zero (fixed), one or two equal signs. The examples include the entire string value - if any other characters occur in the string then it's a keeper.
trash:
AAAAAAAA
AAAAAAAA=
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==
AAAAAAAAAAAAA=
keepers:
AAAAA
AAAAA=
AAAAA0AAAAAAAAAAAAAAAAAAAAAAAAAAA
==
I'm lame at regular expressions, so request some help.
What expression will permit me to take out the trash?
Thanks!
Try using: ^A{8,}={0,2}$
Demo (JavaScript):
var regex = /^A{8,}={0,2}$/
console.log([
// Trash (true)
'AAAAAAAA',
'AAAAAAAA=',
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==',
'AAAAAAAAAAAAA=',
// Keep (false)
'AAAAA',
'AAAAA=',
'AAAAA0AAAAAAAAAAAAAAAAAAAAAAAAAAA',
'=='
].map(regex.test, regex))
Assuming you need eight or more As, followed by zero or more equals signs, you can use:
[A]{8,}[=]{0,}
Note that this will also match the final set of A's in AAAAA0AAAAAAAAAAAAAAAAAAAAAAAAAAA. If you don't want that to match, you should start and end the regex with the delimiters ^ and $:
^[A]{8,}[=]{0,}$
Hope this helps! :)
This regexp may work for you: ^A{8}[A=] if matched, it's a trash value.

Regex: This range OR that range

So I am trying match a certain postcode range:
CB1 *, CB2 *, CB3 *, CB4 *, CB5 *, CB21 *, CB22 *, CB23 *, CB24 *, CB25 *
So I am trying to use range 1-5 OR 21-25.
This is my current regex:
^[CBcb].([1-5]|[21-25]).+$
I want to make sure the post code parts contains the following
[CB OR cb],[1-5 OR 21-25] and [Any combination]
Have a tinker: https://regex101.com/r/aP9uG3/2
How do you do you specify two ranges?
Since the patterns are the same and it is just the 2 that may or may not occur, you can say something like:
CB2?[1-5] # add ^ and $ if required
If you want to specify two ranges, you can always group them with parentheses common_pattern(pattern1|pattern2).
Your Regex pattern:
^[CBcb].([1-5]|[21-25]).+$
is being interpreted as:
^[CBcb].([12345]|[2125]).+$
You need:
^CB2?[1-5].+'
here ? means zero or one match of the preceding token, 2 in this case.
^cb2?[1-5].+$ and use the i flag as well.
The first error was that you were only matching one character from the list [cbCB]. The second is that there's a strange . in the middle. And the third is that you do not specify a range of numbers, but a range of characters. 21 is not a character, it is a sequence of characters. A range of characters to get all possible (integer) numbers would be [0-9]*. What you want is an optional 2 followed by a character from the range [1-5].
You should read up on what lists and ranges are and mean in Regular Expressions because you misused both of them! Eeryone makes mistakes obviously, but this is one of the basics you should get a hang of.
Having characters inside [] makes it a character class. This means that in matches any character inside the brackets (unless it's negated). It doesn't understand numbers, but characters.
If you want to match CB or cb, you separate them by | like CB|cb. Or even better - make your regex case independent. This is done in different ways in different regex flavors. In javascript for example, attach the character i to the regex: /cb/i.
As for the rest of the pattern, if 1-5 and 20-25 is literally what you want, matching 1-5 is done with a character class (which you now are familiar with ;) like [1-5] meaning match any character in the ASCII range between the characters 1 and 5 inclusive.
Make the preceding 2 optional, and your regex looks like this
CB2?[1-5]
It matches your postcode and without a terminating $, it allows for your [Any combination].
Hope this helps.
Regards

blank spaces, number must start with +

I need to get a regex where a phone number must begin with a +. There can be a comma seperated list eg
List:
tel1: +E1234498912345678#fake.com, tel2: +498912345678, tel1: +E123449D1238912345678#fake.com
is a valid list. E is a valid special case
My regex is this:
^(tel1:)|(tel2:)( )(\+.)$
but it accepts numbers without a + as being valid which is not what I want. The number MUST be preceded by a + otherwise it's invalid. Any hints?
You can try the following:
(tel[12]:\s*\+[eE]?\w+(#\w+(\.\w+)+)?(,\s*)?)+
This should match telephone numbers separated by a comma, in a single line.
It also oversees the use of a special character E or e.
Also, domains may not only end in .com. .net or .com.uk should also be valid.