Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

Regular expression for strings and negative number inputs

Greetings, I'm having trouble nailing down the two regular expressions which successfully meet the following conditions:

The first regular expression should accept an alphanumeric string of length 255, starting with a non-numeric or special character.  Or, only -1.  Or only -2.
I've tried, unsuccessfully:  ^[a-zA-Z0-9]*|-1|-2$ as -12 is accepted
Valid inputs:
Some text here
Hi 45 my name is
-1
-2

Invalid Inputs
-1is my name
1is my name
888777
-11
-3

The second regular expression should accept a positive numeric sequence of length 4.  Or, only -1.  Or only -2.
I've tried ^[0-9]{4}|-1|-2$ but it seems -12 is accepted.
Valid inputs:
0000
9999
-1
-2

Invalid Inputs
99999
88
-11
-3

Thanks in advance for your assistance.
0
allenanderson
Asked:
allenanderson
  • 4
  • 3
  • 2
2 Solutions
 
Gurvinder Pal SinghCommented:
for second

[0-9]{4}|\-[1-2]


0
 
Gurvinder Pal SinghCommented:
for first

\D{1}[a-zA-Z0-9]{1-254}|\-[1-2]

but i have a confusion, wont '-1is my name' be the valid input since you said
<<The first regular expression should accept an alphanumeric string of length 255, starting with a non-numeric or special character>>

Also i am not sure, if 255 is the max length or only length to be accepted
0
 
allenandersonAuthor Commented:
gurvinder372,
Thanks for the quick reply.  I made a typo.  For the first regular expression, it should start with a non-numeric and non-special character.  So only lower or upper case letter.
Thanks again!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Gurvinder Pal SinghCommented:
then try

[a-zA-Z]{1}[a-zA-Z0-9]{1-254}|\-[1-2]

0
 
allenandersonAuthor Commented:
gurvinder372.  Thanks again for your help.  Quick follow up.
[0-9]{4}|\-[1-2]:  1245a6 is successful as 1245 matches the [0-9]{4}.  This should fail.   Any thoughts on how, through the regular expression, to not allow input with length more than 4.
[a-zA-Z]{1}[a-zA-Z0-9]{1-254}|\-[1-2] seems to require 254 characters?  Can i get around this by just using * ?  Or, any other thoughts how to range the length between 0 and 255.
Thanks again.
0
 
käµfm³d 👽Commented:

Regex 1:

^(?:-[12]|[a-zA-Z][a-zA-Z0-9 ]{0,254})$

Regex 2:

^(?:-[12]|\d{4})$

Open in new window

0
 
Gurvinder Pal SinghCommented:
which platform are you using? java or javascript?

give the starting character and end char

^[0-9]{4}|\-[1-2]$

and

[a-zA-Z]{1}[a-zA-Z0-9]{1-254}|\-[1-2]
should work for characters length 2-255 since i have given {1,254} which means anywhere from 1 to 254
0
 
käµfm³d 👽Commented:
Correction:
Regex 1:

^(?:-[12]|(?:[a-zA-Z][a-zA-Z0-9 ]{0,254})?)$

Regex 2:

^(?:-[12]|\d{4})$

Open in new window

0
 
käµfm³d 👽Commented:
NP. Glad to help  :)
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now