matched characters - regular expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting matched characters - regular expression
# 1  
Old 06-16-2011
matched characters - regular expression

Hi,

I read the book of <<unix shell programming>>. The regular expression ^\(.\)\1 matches the first character on the line and stores it in register 1. Then the expression matches whatever is stored in the register 1, as specified by the \1. The net effect of this regular expression is to match the first two characters on a line if they are both the same character.

I don't fully understand this regular expression, especially "Then the expression matches whatever is stored in the register 1, as specified by the \1." Can someone explain it in detail and more clearly.

Thanks!
# 2  
Old 06-16-2011
If you surround a block within a regular expression with escaped parenthesis (or un-escaped parenthesis when using Perl compatible regex eg. egrep) you are asking the regex parser to remember what was just matched and store it as the next back reference. Thus
Code:
 grep '^\(.\)\1' file

will match any character at the start of a line (the . character)
and store it in the first back reference which can be addressed as \1.
a more obvious example might be where we had a file with records of the form
user homeNode
and we wished to create an internal mailing list
Code:
sed s'/^([^ ]+) ([^ ]+)$/\1@\2/' users_file.txt

This would print out a series of email addresses

Then again re-reading that I'm not sure if it helps, the following may show it more clearly, assuming you've looked at alternation

Another example would be if you wished to match all of a html tag that could contain a ">" character in it (eg. <img alt="Next>" src="/images/next_button.gif"/>)
Code:
<([^>]+|(["'])[^\2]+\2)+>

Here we match anything that is not a ">" character, or anything that is a quoted string which uses either single or double quotes. We capture the quote type in the second set of parenthesis, (the first being the alternation), and then keep matching characters until the end of the quoted string is marked by the quote we previously matched.

Last edited by Skrynesaver; 06-21-2011 at 07:42 AM.. Reason: Second simpler? example
# 3  
Old 06-16-2011
Hi Skrynesaver,

Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed , awk script for printing matched line before regular expression

hi All , I am having a large file with lots of modules as shown below ############################################### module KKK kksd kskks jsn;lsm jsnlsn; Ring jjsjsj kskmsm jjs endmodule module llll 1kksd11 k232skks j33sn;l55sm (6 Replies)
Discussion started by: kshitij
6 Replies

2. Shell Programming and Scripting

perl regular expression to remove the special characters

I had a string in perl script as below. Tue Augáá7 03:54:12 2012 Now I need to replace the special character with space. After removing the special chaacters Tue Aug 7 03:54:12 2012 Could anyone please help me here for writing the regular expression? Thanks in advance.. Regards, GS (1 Reply)
Discussion started by: giridhar276
1 Replies

3. UNIX for Advanced & Expert Users

Add a line after last matched expression

I thought this would be easy to Google, but I am having trouble getting a clean result that I can understand. I simply want to insert the the line: My Network 192.168.1.1 After the last line that begins with ACL localnet (15 Replies)
Discussion started by: glev2005
15 Replies

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

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

6. Shell Programming and Scripting

How can I get the matched text when using regular expression.

Hello: (exp) : match "exp",the matched text is stored in auto named arrays. How can I get the matched text ? What is the name of the auto named arrays on linux shell ? (4 Replies)
Discussion started by: 915086731
4 Replies

7. Shell Programming and Scripting

Hidden Characters in Regular Expression Matching Perl - Perl Newbie

I am completely new to perl programming. My father is helping me learn said programming language. However, I am stuck on one of the assignments he has given me, and I can't find very much help with it via google, either because I have a tiny attention span, or because I can be very very dense. ... (4 Replies)
Discussion started by: kittyluva2
4 Replies

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

9. Shell Programming and Scripting

Regular Expression to match repeated characters

Hello All I have file which contain sample data like below - test.txt ---------------------------------------------- jambesh aaa india trxxx sdasd mentor asss light train bbblah --------------------------------------------- I want to write a regX which would print only those... (4 Replies)
Discussion started by: jambesh
4 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