Quick regex question about alphabetic string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Quick regex question about alphabetic string
# 1  
Old 09-06-2008
Quick regex question about alphabetic string

Hi guys,

Pretty new to regex, and i know im doing something wrong here. I'm trying to get a regex command that restricts a string to be 8 characters long, and the first character cannot be 0. Here's what i have so far...

Code:
echo "01234" | grep "^[0][0-9]{8}*$"

Thanks very much!

-Crawf

EDIT: Sorry about the wrong title, after two long hours of trying another regex command to work, about 30 seconds after posting managed to figure out an answer by myself...Sorry!

Last edited by crawf; 09-06-2008 at 04:07 AM.. Reason: Solved question
# 2  
Old 09-06-2008
no need regular expression. KISS
Code:
[[ ${#num} -ne 8 ]] && echo "not equal 8 characters" && exit
case $num in
 0* ) echo "0 as first character""  &&     exit ;;
esac

# 3  
Old 09-06-2008
Aha! That works as well!

Thanks mate!

-Crawf
# 4  
Old 09-06-2008
If you are using ksh93, the following is one way of testing the numeric string is of length 8 and does not have a leading zero
Code:
#!/bin/ksh93

num="12345678"
req_len=8

if [[ ${#num}-$num == $req_len-${num/?('0')({7,8}(\d))*/\2} ]]
then
   print "OK - $num matches criteria"
else
   print "Sorry - $num contains either a leading zero or is not of length ${req_len}!"
fi

# 5  
Old 09-06-2008
Can that be done in the Bourne shell? Thats what i'm working in...

Looks just what i need though! Thanks!

-Crawf
# 6  
Old 09-06-2008
$echo "01234567"|awk 'BEGIN{FS=null}{if(length==8 && $1!='0') print"Valid"}'
$echo "91234567"|awk 'BEGIN{FS=null}{if(length==8 && $1!='0') print"Valid"}'
Valid
# 7  
Old 09-06-2008
Thanks Abhishek Ghose! That works well too!

Thanks for the support guys!

-Crawf
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Quick help on Understanding sed Regex

Hi Guys, Could you please kindly explain what exactly the below SED command will do ? I am quite confused and i assumed that, sed 's/*$/ /' 1. It will remove tab and extra spaces .. with single space. The issue is if it is removing tab then it should be Î right .. please assist.... (3 Replies)
Discussion started by: Nandy
3 Replies

2. Homework & Coursework Questions

Function to Check if string input from user is alphabetic only

Good Evening. I'm new to C. Can you please help me. I'm creating an error checking function, user will input a string, this will check if the input is all alphabet or all letters only. If there is a digit or other special char, it will print Error then ask input from user again. Here's my... (1 Reply)
Discussion started by: eracav
1 Replies

3. Shell Programming and Scripting

filtering out duplicate substrings, regex string from a string

My input contains a single word lines. From each line data.txt prjtestBlaBlatestBlaBla prjthisBlaBlathisBlaBla prjthatBlaBladpthatBlaBla prjgoodBlaBladpgoodBlaBla prjgood1BlaBla123dpgood1BlaBla123 Desired output --> data_out.txt prjtestBlaBla prjthisBlaBla... (8 Replies)
Discussion started by: kchinnam
8 Replies

4. Shell Programming and Scripting

Perl: Any quick way to use regex on hash keys?

Hi, Is there any quick way to use pull out keys that match a specific regex pattern? eg %hash ; $hash(123,456) = xxx; $hash(123,457) = xxx; $hash(123,458) = xxx; $hash(223,459) = xxx; I need a fast way to get all the keys that start with 123.. Meaning I should get ... (5 Replies)
Discussion started by: Leion
5 Replies

5. Shell Programming and Scripting

Quick regex question

Say that I want to match any of the following: abc def ghi The letters will either be "abc", "def", or "ghi", only those three patterns. The numbers will vary, but there will only be numbers between the brackets. I've only been able to match abc, using the following: abc.*. I'm... (1 Reply)
Discussion started by: retrovertigo
1 Replies

6. UNIX for Dummies Questions & Answers

quick question

from command prompt I did grep two words on a same line for eg: grep abc | grep xyz and I got tht particular line, but I want to know when I vi that file how to directly search for that particular line? I appreciate if any one can provide answer, thanks in advance (2 Replies)
Discussion started by: pkolishetty
2 Replies

7. Shell Programming and Scripting

Ok quick question

Hi i just wanted to know is there anyway to log the keystrokes on a remote computer? For example i let my nieces play on my other computer downstairs *my computer and the one downstairs are on a LAN* and i want to see everything they type in to make sure they arent doing anything they are supposed... (1 Reply)
Discussion started by: Corrail
1 Replies

8. UNIX for Dummies Questions & Answers

Quick Question

Hi, I am new to UNIX, and am learning from this tutorial : http://www.ee.surrey.ac.uk/Teaching/Unix/index.html It keeps telling me to files downloaded from the internet (like .txt files) to the directory, and I dont know how to. How do I add .txt files to my directory? Thanks. (6 Replies)
Discussion started by: IAMTHEEVILBEAN
6 Replies

9. UNIX for Dummies Questions & Answers

Another quick question

Hi guys sed -e "s/$<//g" the $< can allow me to assign an input value to the variable right? do the double quotes check the previous context? (1 Reply)
Discussion started by: hamoudzz
1 Replies

10. UNIX for Dummies Questions & Answers

quick question

hi guys trying to understand what this line means sed is a stream editor and i understand that, i have a file already selected i want to edit so i use -e sed -e the next stesp is s/$* s is a subsititute replacement sed -e s/$*//g $ is in reference of the last line /g makes it... (2 Replies)
Discussion started by: hamoudzz
2 Replies
Login or Register to Ask a Question