Regex array of number [duplicate] - regex

This question already has answers here:
Regex validation for numbers with comma separator
(3 answers)
Closed 4 years ago.
I have a problem with regex and need your help. I want to check my string is correct or incorrect. First and last is a number, only number and comma after it. No space inside 2 numbers.
Ex:
1,2,3,49,5 this is correct
1,2,3,45, this is incorrect
,12,4,2,67 this is incorrect
1,2 3,4,5,6 this is incorrect

^(?:\d+,)*\d+$
(?:\d+,)* - gets matches like "0," "00," "000," ... or empty
\d+ - gets last number as "0" "00" "000"

Please check below regex to solve your problem.
Regex: ^[0-9]+([0-9,])+[0-9]+$
^[0-9]+ is for start with one or more number
[0-9]+$ is for end with one or more number
([0-9,])+ is for one or more number with comma
Please check the output in Regex101
Update:
Please check the updated regex: ^(\d+,)+\d+$
^(\d+,)+ is for one or more number with comma and this will handle first number with comma
\d+$ is for end with one or more number
Please check the updated output in Regex101

Related

Multi-part Regular Expression [duplicate]

This question already has answers here:
Comma Separated Numbers Regex
(6 answers)
Closed 3 years ago.
I have the requirement to restrict a non-required textbox to only numbers (0-9) with a separator of ';'. The pattern is that the groups can be 4 or 5 in length and can repeat n times. Anything less than 4 in a group is invalid. After 4 or 5 I need to check for the existence of a separator character ';'. This pattern can repeat n times. I have tried variations of but this doesn't seem to be working. Something simple to start out like
[0-9]{4,5};+
is invalid as I don't need the separator for only 1 number grouping.
Next I tried
^[0-9]{4,5}|[0-9]{4,4};|[0-9]{5,5};$
but this doesn't work because the existence of four digits like 1111 or five digits 11111 before gives one match before it goes awry example "11111;j" Is there a way in a regex to validate
1111
11111
1111;1111
11111;1111
11111;11111
but catch
111
111;
1111;1
11111;1
abc
in a repeating fashion?
This validate your examples.
^[0-9]{4,5}(;[0-9]{4,5})?$
Try it
It's not clare what you mean by "in a repeating fashion". If you want validate also this
1111;11111;11111;1111;11111
You can use this regex
^[0-9]{4,5}(;[0-9]{4,5})*$
Try it

Regular expression splunk query [duplicate]

This question already has answers here:
Getting the text that follows after the regex match
(5 answers)
Closed 4 years ago.
I have a line containing
[India,sn_GB] Welcome : { Name:{Customer1},Place:{Mumbai},}
I want to print the entire line after sn_GB] in splunk, which is
Welcome : { Name:{Customer1},Place:{Mumbai},}
I used the below regular expression:
(?<=sn_).*?$
But it prints, along with GB] like GB] Welcome : { Name:{Customer1},Place:{Mumbai},}.
In the word sn_GB, sn_ is constant and the rest two letter will vary, like GB, LB, KB, TB as such.
Please help me in correcting the regular expression.
Thanks
This will give the correct result in case sn_GB is constant.
(?<=sn_GB).*?$
If GB is not constant you can go for:
(?<=sn_...).*?$
I understand your question now.
Country codes are always 2 letters.
i'd use
(?<=sn_..\]\ ).*$
but you could use
(?<=sn_[A-Z]{0,5}\]\ \s*).*?$
(?<=sn_....).*$
is the simplest, as it will just grab 4 characters after, if it's always 2 letters for country code, and then a closing bracket and a space

regex to check for subsequent characters [duplicate]

This question already has answers here:
What's a Regex pattern for 3 consecutive digits increasing or decreasing
(3 answers)
Closed 6 years ago.
i would like to ask if it is possible to get a regex to check for subsequent alphabets or numbers in a string such as "abcd" or "1234" 4 characters in succession.
This is for password validation to check if a user is trying to enter a password like "abcd1234"
Thank You
Based on the answer Match increasing/decreasing sequences using regex, this pattern does close. Through programming you still need to get the first capturing group \1 and check whether its size is equal to 4. Here you need to set the multi line mode, to allow $ match the end of line.
This is because this regex is capturing all possible groups, however the groups you want has the length of 4, so it should not be a problem.
(?x)
(
(?:a(?=b|$))?
(?:b(?=c|$))?
(?:c(?=d|$))?
(?:d(?=e|$))?
(?:e(?=f|$))?
(?:f(?=g|$))?
(?:g(?=h|$))?
(?:h(?=i|$))?
(?:i(?=j|$))?
(?:j(?=k|$))?
(?:k(?=l|$))?
(?:l(?=m|$))?
(?:m(?=n|$))?
(?:n(?=o|$))?
(?:o(?=p|$))?
(?:p(?=q|$))?
(?:q(?=r|$))?
(?:r(?=s|$))?
(?:s(?=t|$))?
(?:t(?=u|$))?
(?:u(?=x|$))?
(?:x(?=z|$))?
[a-z]?
|
(?:0(?=1|$))?
(?:1(?=2|$))?
(?:2(?=3|$))?
(?:3(?=4|$))?
(?:4(?=5|$))?
(?:5(?=6|$))?
(?:6(?=7|$))?
(?:7(?=8|$))?
(?:8(?=9|$))?
\d?
)
On this link you can see the live regex: https://regex101.com/r/xxED4s/2

Regular expression for 7 digit numbers separeted by commas

I need a regular expression to validate a concatenated string that consists of 7 digit numbers separated by commas.
Furthermore, I must ensure that:
The string is not empty.
The chain doesn't begins or finish with commas.
The numbers do not start with 0.
Example: 1234567,2345678,3456789
My solution so far: ^\d+(,\d+)*?$
The problems I still need to resolve:
Validate that the numbers are exactly 7 digits.
Validate that the numbers do not start with 0.
Thank you.
Something like ^[1-9]\d{6}(,[1-9]\d{6})+$ should work. The [1-9] ensures the number doesn't begin with 0, and \d{6} ensures that there are 6 digits to follow.
Based on Gavin answer, here is what worked for me : ^[1-9]\d{6}(,[1-9]\d{6})*$
The minor difference is the use of the * instead of + at the end of the regular expression. There are some cases where I must validate only one 7 digits number...
Thank you for the help everyone!

reformat numbers using regex [duplicate]

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 6 years ago.
I have a file that contains any of the following number format
12.456.7890
12-456-7890
123.456.7890
(123)456.7890
(123).456.7890
123-456-7890
(123)-456-7890
(123)456-7890
Is it possible to use regex substitution so that the final output number will always be on a format (123)456-7890 or (12)456-7890
Yes, it is:
s/\(?(\d\d\d)\)?[-.]?(\d\d\d)[-.]?(\d\d\d\d)/($1)$2-$3/g
I should mention that the above will also parse the following two:
123)456.7890
(123456.7890
You can do this using two substitutions:
perl -lpe 's/\D//g; s/(\d{3})(\d{3})(\d{4})/($1)$2-$3/' file
The first one removes all characters that aren't numeric. The second one inserts the desired characters between each group.
You should take into account that this approach will make a mess of any lines that aren't like the ones in your sample input. One means of protecting yourself could be something like this:
if ((#a = /\d/g ) == 10) { /* perform substitutions */ }
i.e. ensure that the number of matches on the line is 10 before proceeding.