Solved

Regular expression for strings and negative number inputs

Posted on 2010-09-20
9
283 Views
Last Modified: 2012-06-27
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
Comment
Question by:allenanderson
  • 4
  • 3
  • 2
9 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33718082
for second

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


0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33718122
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
 

Author Comment

by:allenanderson
ID: 33718260
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
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33718284
then try

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

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:allenanderson
ID: 33718510
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 33718737

Regex 1:

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

Regex 2:

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

Open in new window

0
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 150 total points
ID: 33718743
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
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 350 total points
ID: 33718760
Correction:
Regex 1:

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

Regex 2:

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

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 33724934
NP. Glad to help  :)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java Regular Expression 5 43
Regex or operator problems 8 35
numeric only entry in text inpu 9 39
Regex code,how to do this? 3 24
by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now