# Quick regex question about alphabetic string

**URL:** <https://community.unix.com/t/quick-regex-question-about-alphabetic-string/211432>\
**Category:** Shell Programming and Scripting\
**Created:** [September 6, 2008, 2:58am UTC](https://community.unix.com/t/quick-regex-question-about-alphabetic-string/211432 "2008-09-06T02:58:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![crawf](https://community.unix.com/letter_avatar/crawf/32/5_5575768a8748004e209b776fc1b2916d.png) [@crawf](https://community.unix.com/u/crawf)\
**Post date:** [September 6, 2008, 2:58am UTC](https://community.unix.com/t/quick-regex-question-about-alphabetic-string/211432/1 "2008-09-06T02:58:23Z")

</div>

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...

```nohighlight
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!

---

<div class="post-metadata">

**Author:** ![ghostdog74](https://community.unix.com/letter_avatar/ghostdog74/32/5_5575768a8748004e209b776fc1b2916d.png) [@ghostdog74](https://community.unix.com/u/ghostdog74)\
**Post date:** [September 6, 2008, 4:18am UTC](https://community.unix.com/t/quick-regex-question-about-alphabetic-string/211432/2 "2008-09-06T04:18:41Z")

</div>

no need regular expression. KISS

```nohighlight
[[${#num} -ne 8]] && echo "not equal 8 characters" && exit
case $num in
 0* ) echo "0 as first character"" && exit ;;
esac

```

---

<div class="post-metadata">

**Author:** ![crawf](https://community.unix.com/letter_avatar/crawf/32/5_5575768a8748004e209b776fc1b2916d.png) [@crawf](https://community.unix.com/u/crawf)\
**Post date:** [September 6, 2008, 4:21am UTC](https://community.unix.com/t/quick-regex-question-about-alphabetic-string/211432/3 "2008-09-06T04:21:19Z")

</div>

Aha! That works as well!

Thanks mate!

-Crawf

---

<div class="post-metadata">

**Author:** ![fpmurphy](https://community.unix.com/user_avatar/community.unix.com/fpmurphy/32/110_2.png) [@fpmurphy](https://community.unix.com/u/fpmurphy)\
**Post date:** [September 6, 2008, 8:54am UTC](https://community.unix.com/t/quick-regex-question-about-alphabetic-string/211432/4 "2008-09-06T08:54:22Z")

</div>

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

```nohighlight
#!/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

```

---

<div class="post-metadata">

**Author:** ![crawf](https://community.unix.com/letter_avatar/crawf/32/5_5575768a8748004e209b776fc1b2916d.png) [@crawf](https://community.unix.com/u/crawf)\
**Post date:** [September 6, 2008, 6:45pm UTC](https://community.unix.com/t/quick-regex-question-about-alphabetic-string/211432/5 "2008-09-06T18:45:48Z")

</div>

Can that be done in the Bourne shell? Thats what i'm working in...

Looks just what i need though! Thanks!

-Crawf

---

<div class="post-metadata">

**Author:** ![Abhishek\_Ghose](https://community.unix.com/letter_avatar/abhishek_ghose/32/5_5575768a8748004e209b776fc1b2916d.png) [@Abhishek\_Ghose](https://community.unix.com/u/Abhishek_Ghose)\
**Post date:** [September 6, 2008, 7:15pm UTC](https://community.unix.com/t/quick-regex-question-about-alphabetic-string/211432/6 "2008-09-06T19:15:17Z")

</div>

$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

---

<div class="post-metadata">

**Author:** ![crawf](https://community.unix.com/letter_avatar/crawf/32/5_5575768a8748004e209b776fc1b2916d.png) [@crawf](https://community.unix.com/u/crawf)\
**Post date:** [September 6, 2008, 7:17pm UTC](https://community.unix.com/t/quick-regex-question-about-alphabetic-string/211432/7 "2008-09-06T19:17:06Z")

</div>

Thanks Abhishek Ghose! That works well too!

Thanks for the support guys!

-Crawf

---

<div class="post-metadata">

**Author:** ![era](https://community.unix.com/letter_avatar/era/32/5_5575768a8748004e209b776fc1b2916d.png) [@era](https://community.unix.com/u/era)\
**Post date:** [September 7, 2008, 7:38am UTC](https://community.unix.com/t/quick-regex-question-about-alphabetic-string/211432/8 "2008-09-07T07:38:52Z")

</div>

```nohighlight
case $num in [1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) echo fine;;
  *) echo not good at all, play again;;
esac

```
