LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 05-28-2008, 03:11 PM   #1
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Rep: Reputation: 55
Need help with a regular expression


How can I match at least 1 non-digit, followed by 4 digits with a perl compatible regular expression?

I have:
\D{1}\d{4}
but thats matching 4 digits, 5 digits, 6 digits, etc.
 
Old 05-28-2008, 03:53 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by abefroman View Post
but thats matching 4 digits, 5 digits, 6 digits, etc.
Of course it does. This is because one non-digit followed by 5 digits is also one non-digit followed by 4 digits. If you were capturing the expression, you would notice that only the four digits you want will be captured.

Otherwise you want to revise your criteria. What it seems you want is one non-digit, followed by 4 digits, with the proviso that if you look ahead, there will be 0 or more non-digits. So
Code:
\D\d{4}(?=\D*)
You don’t really need the look-ahead unless your are capturing, so this will work just as well in a simple truth test:
Code:
\D\d{4}\D*
 
Old 05-28-2008, 09:17 PM   #3
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Original Poster
Rep: Reputation: 55
Cool

Thanks!
 
  


Reply



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
regular expression [tT]h* sycamorex Programming 3 01-15-2008 04:43 PM
Regular expression datbenik Programming 1 01-05-2006 01:58 PM
Need help with regular expression aecaudel Programming 6 11-04-2005 05:28 AM
Regular Expression Help WeNdeL Linux - General 1 08-14-2003 10:08 AM
Regular Expression slizadel Programming 4 07-28-2003 05:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:38 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration