LinuxQuestions.org
Help answer threads with 0 replies.
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 07-23-2010, 09:11 AM   #1
Daravon
Member
 
Registered: Mar 2006
Posts: 170

Rep: Reputation: 15
Regex to match C-1234567-01-A3


At work, I run the product scriber. The scriber scribes the product number on the product. A typical product number is

C-1234567-01-A3 (for example)

The C and the hyphens never change. So basically all valid scribes are of the form

C-*some seven numerical digits*-*some two numerical digits*-*some alphanumeric checksum*

I'm not a programmer but I need to ask the programmer to check the scribe's form. I don't want to actually read the scribe and see if it's correct, I just need to verify the syntax, so that we can raise a flag if there's no way it could be correct.

I want to communicate the requirements for a valid scribe syntax with a regex so that later there's no ambiguity about what I asked for. Problem is I don't know how to express it precisely. I originally came up with

C-[0-9]-[0-9]-[A-Z0-9]

This says what the 3 fields can contain, but it doesn't express the fact that the first number must be exactly 7 numeric digits, and the sequence number must be exactly 2, and the checksum must be exactly 2 alphanumeric digits.

How can I communicate these requirements unambiguously in a form that any programmer will understand?
 
Old 07-23-2010, 09:20 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
$ var="test C-1234567-01-A3 test"
C-1234567-01-A3
$ echo $var | grep -oE "C-[0-9]{7}-..-.."
C-1234567-01-A3
$ var="test C-123456-01-A3 test"
$ echo $var | grep -oE "C-[0-9]{7}-..-.."
 
Old 07-23-2010, 09:35 AM   #3
Daravon
Member
 
Registered: Mar 2006
Posts: 170

Original Poster
Rep: Reputation: 15
I didn't expect an answer in the form of code; I'm afraid I don't speak bash. Maybe if it was perl...

I think I see what you did there partly though, so maybe what I'm looking for is

Code:
C-[0-9]{7}-[0-9]{2}-[A-Z0-9]{2}
?
 
Old 07-23-2010, 09:41 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
You are correct!!

The only other thing to note is that ghostdog put the -E flag in the grep statement. This means "use extended regular expression (ERE) syntax".

(I don't recall what about the expression is ERE, but you can read up on it in any good tutorial---eg: http://www.grymoire.com/Unix/Regular.html
 
Old 07-23-2010, 10:01 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
I don't recall what about the expression is ERE
The curly bracketed grouping would need backslashes and stuff without the -E
 
Old 07-23-2010, 10:07 AM   #6
Daravon
Member
 
Registered: Mar 2006
Posts: 170

Original Poster
Rep: Reputation: 15
Cool, I created a text file containing a bunch of phony product numbers and one good product number, and now I'm using grep to test my regex. Surprisingly it pulled out a number I intended as a phony. I'm not sure it's a problem though, because checksums might not always contain letters. I'm not sure about that. I guess I need to know more about my checksum.

Code:
chaz@jarvis:~$ cat scribes.txt
C-1234567-01-A3 B-1234567-01-A3 C-1234567-A1-A3 C-123X567-01-A3 c-1234567-01-A3 C-1234567-01-03
chaz@jarvis:~$ cat scribes.txt | grep -oE "C-[0-9]{7}-[0-9]{2}-[A-Z0-9]{2}"
C-1234567-01-A3
C-1234567-01-03
chaz@jarvis:~$
 
  


Reply


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
[SOLVED] Simple regex match without replace? nrg Programming 1 11-21-2009 03:04 AM
What should some Regex match in awk? sebelk Programming 7 11-20-2009 06:38 PM
Help with regex to match an IP abefroman Linux - Server 5 11-02-2009 03:07 PM
Extract everything before a regex match onesikgypo Programming 5 10-21-2009 04:49 AM
help me match this regex line (easy) JustinHoMi Programming 7 03-17-2002 01:43 AM

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

All times are GMT -5. The time now is 03: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