validating input using regular expressions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting validating input using regular expressions
# 1  
Old 02-27-2006
Data HELP: validating IP addresses any way possible

I am trying to validate input from the user in a script. I thought is was easy to do using regular expressions but I can't figure out how to use REs in a conditional. I have tried using grep from a temp file, sed from a temp file, sed from command line, comparison in an if condition and I cannot figure it out and I don't have much hair left! i am trying to validate input of an IP address.

Trying to do:

if [[ $IP = \b([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\b ]];then
match=yes
else
match=no
fi

Thanks for your help

OK I have tried the following expressions that according to the regex tool match IP addresses, but they all seem to flip flop back and forth for valid and not valid IPs and the entire list albeit 2 or 3 are valid. What is the deal. I would gladly do this another way if anyone could steer me in the right direction. Thanks greatly for your help!!
rc=`egrep -c "[0-9][13]\.[0-9][13]\.[0-9][13]\.[0-9][13]"`

rc=`egrep -c \\b\(?:\(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\)\
\.\){3}\(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\)\\b"`

rc=`egrep -c '\\b\(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\)\\.\(2
5[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\)\\.\(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\)\\.\(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\)\\b'

rc=`egrep -c '10.\([781]|70|80\).\(25[0-5]|2[0-4][0-9]|[01]?[0-
9][0-9]?\)\\.\(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\)\\b'

Last edited by nrodolfich; 02-28-2006 at 12:19 PM..
# 2  
Old 02-27-2006
val="92.91.91.93";

if (( $(echo $val | egrep -c "[0-9][13]\.[0-9][13]\.[0-9][13]\.[0-9][13]") >= 1 ))
then
match="yes"
echo "Found"
else
match="no"
echo "Not found"
fi
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular Expressions

Hi Ilove unix and alwyas trying to to learn unix,but i am weak in using regular expressions.can you please give me a littel brief discription that how can i understand them and how to use .your response could lead a great hand in my unix love. (1 Reply)
Discussion started by: manoj attri
1 Replies

2. Shell Programming and Scripting

Help with regular expressions

I have a file that I'm trying to find all the cases of phone number extensions and deleting them. So input file looks like: abc x93825 def 13234 x52673 hello output looks like: abc def 13234 hello Basically delete lines that have 5 numbers following "x". I tried: x\(4) but it... (7 Replies)
Discussion started by: pxalpine
7 Replies

3. UNIX for Dummies Questions & Answers

Regular expressions

In regular expressions with grep(or egrep), ^ works if we want something in starting of line..but what if we write ^^^ or ^ for pattern matching??..Hope u all r familiar with regular expressions for pattern matching.. (1 Reply)
Discussion started by: aadi_uni
1 Replies

4. UNIX for Advanced & Expert Users

regular expressions

I have a flat file with the following drug names Nutropin AQ 20mg PEN Cart 2ml Norditropin Cart 15mg/1.5ml I have to extract digits that are before mg i.e 20 and 15 ; how to do this using regular expressions Thanks ram (1 Reply)
Discussion started by: ramky79
1 Replies

5. UNIX for Advanced & Expert Users

Regular Expressions

Hi, below is a piece of code written by my predecessor at work. I'm kind of a newbie and am trying to figure out all the regular expressions in this piece of code. It is really a tough time for me to figure out all the regular expressions. Please shed some light on the regular expressions... (3 Replies)
Discussion started by: ramky79
3 Replies

6. Shell Programming and Scripting

Help with regular expressions

I have following content in the file CancelPolicyMultiLingual3=U|PC3|EN RestaurantInfoCode1=U|restID1|1 ..... I am trying to use following matching extression \|(+) to get this PC3|EN restID1|1 Obviously it does not work. Any ideas? (13 Replies)
Discussion started by: arushunter
13 Replies

7. Shell Programming and Scripting

regular expressions

Hi, can anyone advise me how to shorten this: if || ; then I tried but it dosent seem to work, whats the correct way. Cheers (4 Replies)
Discussion started by: jack1981
4 Replies

8. Shell Programming and Scripting

Regular Expressions

How can i create a regular expression which can detect a new line charcter followed by a special character say * and replace these both by a string of zero length? Eg: Input File san.txt hello hi ... (6 Replies)
Discussion started by: sandeep_hi
6 Replies

9. Programming

regular expressions in c++

How do I use the regular expressions in c++? (2 Replies)
Discussion started by: szzz
2 Replies

10. Shell Programming and Scripting

Regular Expressions

I'm trying to parse RichText to XML. I want to be able to capture everything between the '/par' tag in the RTF but not include the tag itself. So far all I have is this, '.*?\\par' but it leaves '\par' at the end of it. Any suggestions? (1 Reply)
Discussion started by: AresMedia
1 Replies
Login or Register to Ask a Question