What is the meaning of this regular expression?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What is the meaning of this regular expression?
# 1  
Old 02-24-2012
What is the meaning of this regular expression?

I am an amateur in bash scripting and RE. Could someone please tel me in precise English, what is the meaning of the following:
Code:
regex='^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$'

Thanks
# 2  
Old 02-24-2012
Code:
^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])((\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$



There are 4 major sections. The red '^' at the beginning 'anchors' the pattern to the beginning of the data. The expression will only evaluate to true if the first character of the pattern is the first character of the data.

The red '$' at the end is also an anchor which anchors the match to the end of the data. The expression will evaluate to true only if the last character of the matched string is the last character of the data.

There are two major sections in between (blue and black). The blue section consists of a group which is contained within parens. Within the group are pipe '|' separated fields; the pipe indicates an OR condition such that the pattern is considered a match when any single field's expression is true.

The blue group contains a set of 4 fields which will evaluate to true when the chararacters at the beginning
of the string match:

Code:
 [1-9]           one number 0-9
OR
 [1-9][0-9]     two numbers one number in the set 1-9 followed by a number in the set 0-9
OR
 1[0-9][0-9]    three numbers 1 followed by a number in the set 0-9 followed by a number in the set 0-9
OR
 2[0-4][0-9]    three numbers 2 followed by a number in the set 0-4 followed by a number in the set 0-9
OR
  25[0-5]        three numbers 2 then 5 followed by a number in the set 0-5



The second section (black) contains a nested group. The inner group is similar to the blue group. The first number is allowed to be 0 even if a single digit, and the set of up to three digits must be preceded with a dot. There is a backslant (\) in front of the dot because the dot has special meaning and to indicate that a true (real) dot is to be matched it must be escaped.

The outer set of parens in the second group is followed by {3} which is a repetition operator that affects the adjacent group. The 3 indicates that this pattern must exist exactly three times.

I haven't completely vetted it, but it appears that the intent is to match an IP address.
These 2 Users Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Regular expression help

Hi, I am quite knew to scripting and I am trying to get a regular expression to work to check that a user enters a valid version number such as 1 or 1.1 or 12.3 etc. I dont seem to be able to get it to work as it picks up versions such as 1.......2. I only want it to work with a single dot.... (12 Replies)
Discussion started by: frodo61
12 Replies

2. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

3. UNIX for Dummies Questions & Answers

What is the meaning of this awk expression using sub?

Cannot understand what the "&" in the sub expression is supposed to do. {sub(/^/,"&" s)} (3 Replies)
Discussion started by: kristinu
3 Replies

4. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

5. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

6. Shell Programming and Scripting

regular expression (.*?)

hi all, i have a text file with below content ............................... ............................... ............................... ............................... %%Page: (4) 4 %%PageBoundingBox: 34 -30 584 831 %%BeginPageSetup %%BeginFeature: *PageSize A4 595 842... (7 Replies)
Discussion started by: uttamhoode
7 Replies

7. Linux

Regular expression to extract "y" from "abc/x.y.z" .... i need regular expression

Regular expression to extract "y" from "abc/x.y.z" (2 Replies)
Discussion started by: rag84dec
2 Replies

8. Programming

help in regular expression

Hi all, I'm working with flex (version 2.5.4a) on GNU/linux. I need to frame a regular expression which would match cases where word "file" does not occur. Negated character class wont work for me because they enforce "or" clause between different chars (so something like wont work). I would like... (5 Replies)
Discussion started by: Rakesh Ranjan
5 Replies

9. UNIX for Dummies Questions & Answers

Need help in Regular Expression

I have a file with data that looks like - record nullable { final_delim=end ,delim="~%%~" ,quote=none } ( 1_UPC:string; 2_QUANTITY:string; ) I want to fetch the first column that starts with integer. e.g - 1_UPC, 2_QUANTITY. I tried "awk -F ":" -v var1="^0-9" '$1==var1' inschemafile".... (2 Replies)
Discussion started by: mahabunta
2 Replies

10. Shell Programming and Scripting

Regular Expression + Aritmetical Expression

Is it possible to combine a regular expression with a aritmetical expression? For example, taking a 8-numbers caracter sequece and casting each output of a grep, comparing to a constant. THX! (2 Replies)
Discussion started by: Z0mby
2 Replies
Login or Register to Ask a Question