LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-23-2011, 01:13 AM   #1
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,220

Rep: Reputation: 164Reputation: 164
Regular expression help


I need a regular expression that would match:
Code:
-5
-39
9
17
but not match
Code:
3-4
 
Old 03-23-2011, 03:19 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
grep "^[-0-9][0-9]*$"

Last edited by Disillusionist; 03-23-2011 at 03:19 AM. Reason: Forgot the *
 
Old 03-25-2011, 06:18 AM   #3
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie
Posts: 1,428

Rep: Reputation: 83
Hya

How about ?
Code:
grep -e '-5\|-39\|9\|17'
Happy Penguins??
 
Old 03-27-2011, 07:13 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
I think the requirement was to match any integer number (plus or minus)

I believe the examples were just that, examples or sample data.
 
Old 03-27-2011, 07:23 AM   #5
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 697

Rep: Reputation: 93
Quote:
Originally Posted by kaz2100 View Post
How about ?
Code:
grep -e '-5\|-39\|9\|17'
Happy Penguins??
Quite happy after a good laugh.

I like that you found a humorous way to show that we didn't
have a good definition of the problem.
 
Old 03-27-2011, 07:29 AM   #6
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 697

Rep: Reputation: 93
Smeezekitty, will Disallusionist's solution work for you?

You don't mention what program you are using or in what
context. A regex that works in grep might give different
results when used in egrep or sed.
 
Old 03-27-2011, 08:17 AM   #7
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Disillusionist's expression would also match "-" on its own, which might not be appropriate.

I would use
Code:
^-?[0-9]+$
or
Code:
^-\?[0-9]\+$
for grep's simple regular expressions. Most regex implementations should support an expression that simple.
 
Old 03-27-2011, 07:57 PM   #8
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
I see that you are just checking for positive or negative numbers? In that case, use < or > operator eg >0 , or <0. No need to use regular expression.
 
  


Reply

Tags
regular expressions


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with regular expression Feyd-Rautha Programming 8 04-21-2008 11:18 AM
Regular Expression msvinaykumar Programming 2 08-14-2006 08:48 AM
Need help with regular expression aecaudel Programming 6 11-04-2005 05:28 AM
Regular Expression slizadel Programming 4 07-28-2003 05:16 AM
regular expression gumby Programming 3 07-15-2003 12:13 PM


All times are GMT -5. The time now is 10:23 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration