Using Regex to validate account numbers - regex

Could you please help me with a regular expression that would allow strings in several valid formats?
The formats are:
5digits dash 6digits
5digits dash 7digits
6digits dash 6digits
7digits dash 7digits
7digits dash 8 digits
I am asking this because I know almost nothing about regex.
Thanks.

I would suggest you to keep it simple to this regex:
/^\d{5,7}-\d{6,8}$/
Even though your specified formats can be validated by following regex:
^\d{5}-\d{6,7}|\d{7}-\d{7,8}|\d{6}-\d{6}$
but that won't be very maintainable for future changes.

Related

Lucene Regex for alphanumeric match but not all numeric

I want to find the alphanumeric words in lucene automata regex but not entirely numeric and even not entirely alphabets.
I have tried
(([a-zA-Z0-9]{1,10})&(.*[0-9].*))
but this returns all numeric words also
So i tried to negate all numeric like below but it does not work
(^[0-9])(([a-zA-Z0-9]{1,10})&(.*[0-9].*))
Input String:
DL200, dal2 , 700091
Expected output:
DL200 and dal2
but it should not return 700091
With the help of the JvdV answer and with the help of https://stackoverflow.com/a/38665819/9758194, I was able to get the desired output
(([a-zA-Z0-9]{1,10})&(.*[0-9].*))&~([0-9]*)
Didn't know much about lucene regex flavor, but a little research tought me that it does not support PCRE library, however some standard operators are supported. I found that it does not include lookarounds nor word boundaries. Have a look at the docs.
Either way, to overcome the lack of support on lookarounds I had a look at this older SO post to use ~ instead. Furthermore, I see you can use the & operator to check if the string matches multiple patterns.
This makes for the assumption the following pattern might work for you:
~[0-9]+&~[^0-9]+&[A-Za-z0-9]{2,10}
~[0-9]+ - Negate a string made of numbers only.
&
~[^0-9]+ - Negate a string made of non-numbers only.
&
[A-Za-z0-9]{2,10} - Matches a string that is made out of 2 to 10 alphanumeric characters.

Yet another password validating regular expression

I've gone through multiple examples to validate passwords via regular expression, but none of them quite fit what I am looking for. I've been using trial and error to build my own, but without complete success.
Here is the regular expression that so far is the closest match for what I am looking for:
(?=.*?[a-z]{3,})(?=.*?[A-Z]{3,})(?=.*?[0-9]{2,})[a-zA-Z0-9]{8,24}
The password should have three lowercase and three uppercase alphabets and two numbers. Password length should be between 8 and 24 characters. Special characters are not looked for, they can be used as long as other requirements are met.
The regular expression above matches ABCdef12 but does not match Ad1Be1Cf. How I should modify the regular expression so it also matches the latter example?
Use look aheads for the content assertion, and a simple regex for the length:
^(?=(.*[a-z]){3})(?=(.*[A-Z]){3})(?=(.*\d){2}).{8,24}$
See demo
I'm reasonably confident this this the shortest regex that will work for you.
(?=.{8,24}$)(?=.*?[a-z].*?[a-z].*?[a-z])(?=.*?[A-Z].*?[A-Z].*?[A-Z])(?=.*?\d.*?\d)(^.*$)
You can use this.It uses lookahead to test all conditions.
See Demo.
http://regex101.com/r/yX3eB5/9

Regex Expression Query

I have data as
CVE-2011-0573,
CVE-2011-0606,
(CVE-2011-0565)
CVE-2011-0598,
CVE-2011-0593.
((CVE-2011-0593.)
Could you please help me writing RegEx so I only get ABC-####-#### ? The last four digit may vary, so it can have e.g. three or five digits, but most likely not more than ten. Also the expresion may contain some spaces in the end, so those need to be removed as well.
You can use this regex for matching:
[A-Z]{3}-[0-9]{4}-[0-9]{3,10}
If you have multiline flag available then use
/^[A-Z]{3}-[0-9]{4}-[0-9]{3,10}$/mg

Regex pattern for a password

I need a regex pattern that validates a password format.
The rules are:
Minimum 8 chars in total
at least two letters
at least two digits or symbols
I came up with the following:
/((?=.*[0-9\#\&#\$\?\%!\|(){}[]])(?=.*[a-zA-Z]).{8,})/
It will look if both occure once, but I need it toch validate if they occur at least twice.
If I add the {2,} like this:
/((?=.*[0-9\#\&#\$\?\%!\|(){}[]]{2,})(?=.*[a-zA-Z]{2,}).{8,})/
Then the following doesnt work for example: a1a1a1a1a1
Can anybody help me?
This is how you do it, using positive lookaheads: http://regex101.com/r/uW0yI4
/^(?=.*[a-z].*[a-z])(?=.*[!"#...\d].*[!"#...\d]).{8,}$/gmi
Just replace !"#... with all the symbols you want to match.
Note: the multiline flag might be unnecessary for your applications.
This should give you what you're after:
^((?=(.*[\d0-9\#\&#\$\?\%!\|(){}[\]]){2,})(?=(.*[a-zA-Z]){2,}).{8,})$

My regular expression allows spaces when formatting phone number

I have the following regex being used in javascript.
phone_number.match(/^1-\d{3}-\d{3}-\d{4}$/);
it works great with one exception. It allows spaces.
I want to strictly format 1-xxx-xxx-xxxx
but it allows 1- xxx-xxx-xxxx
anyone have any ideas how I can NOT allow spaces?
No it does not. ;-)
It is just impossible. In your regexp /^1-\d{3}-\d{3}-\d{4}$/ you do not have any space character - and it does not match (checked).