Regular Expressions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regular Expressions
# 1  
Old 11-02-2009
Power Regular Expressions

Code:
#!/usr/bin/perl


$word = "one last challenge";


if ( $word  =~ /^(\w+).*\s(\w+)$/ )
{
	print "$1";
	print "\n";
	print "$2";

}

The output shows that "$1" is with result one and "$2" is with result challenge. I am confused about how this pattern match expression works step by step. I request u guys` explanation. Thank u very much.

Last edited by pludi; 11-02-2009 at 09:57 AM.. Reason: code tags, please...
# 2  
Old 11-02-2009
you don't need that much regular expression. just split on space
Code:
@s = split /\s+/ , $word;

then go through each element of @s to get your word.
# 3  
Old 11-02-2009
I guess the OPs question isn't about splitting a string, but rather about how regular expressions work (in this case in Perl).

Let's take a look at the regex you have:
  • ^
    Start (Anchor) at the beginning of the string
  • (\w+)
    Match one or more word character greedily, and save the match in a variable for further use.
  • .*
    Match 0 or more of any character greedily, and discard the result.
  • \s
    Match exactly 1 whitespace character
  • (\w+)
    Again, match one or more word character and save the result.
  • $
    Anchor the regex at the end of the string.

Color coded (not shown: the anchors at the beginning and the end; whitespaces shown as '·'):
Code:
^(\w+).*\s(\w+)$
one·last·challenge

# 4  
Old 11-02-2009
try:
Code:
printf %s, (split /\s/, $word)[0]

# 5  
Old 11-02-2009
Hi thank u for your reply. I juz get confused that why ".*\s" does not match "last " Thank you
note that there is a whitespace after double-quoted last

---------- Post updated at 09:18 AM ---------- Previous update was at 09:17 AM ----------

Quote:
Originally Posted by pludi
I guess the OPs question isn't about splitting a string, but rather about how regular expressions work (in this case in Perl).

Let's take a look at the regex you have:
  • ^
    Start (Anchor) at the beginning of the string
  • (\w+)
    Match one or more word character greedily, and save the match in a variable for further use.
  • .*
    Match 0 or more of any character greedily, and discard the result.
  • \s
    Match exactly 1 whitespace character
  • (\w+)
    Again, match one or more word character and save the result.
  • $
    Anchor the regex at the end of the string.

Color coded (not shown: the anchors at the beginning and the end; whitespaces shown as '·'):
Code:
^(\w+).*\s(\w+)$
one·last·challenge



Hi thank u for your reply. I juz get confused that why ".*\s" does not match "last " Thank you
note that there is a whitespace after double-quoted last
# 6  
Old 11-02-2009
Quote:
Originally Posted by DavidHe
...
Hi thank u for your reply. I juz get confused that why ".*\s" does not match "last " Thank you
note that there is a whitespace after double-quoted last
It's pretty simple.

(A) Yes, ".*\s" does match " last ". (i.e. "last" with a space at both ends).
(B) It matched, but was not stored in a variable.

Why not ? Because it was not enclosed within brackets.

If you enclose it within brackets, then $2 will be assigned the value " last ". And, of course, "challenge" will go to $3.

The test is shown below:

Code:
$
$ cat -n tst.pl
     1  #!/usr/bin/perl
     2  $word = "one last challenge";
     3  if ( $word  =~ /^(\w+)(.*\s)(\w+)$/ )
     4  {
     5    print "==>|$1|<==";    # ==>|one|<==
     6    print "\n";
     7    print "==>|$2|<==";    # ==>| last |<==
     8    print "\n";
     9    print "==>|$3|<==";    # ==>|challenge|<==
    10  }
    11
$
$ perl tst.pl
==>|one|<==
==>| last |<==
==>|challenge|<==$
$
$

Note that the value of $2 is "last" with a single space at both ends.
Also note that $3 equals "challenge" without a newline at the end, due to which the $ prompt of the shell shows up right after those "==" characters.

HTH,
tyler_durden
# 7  
Old 11-03-2009
Thank u for your reply.... But doesn`t anything match the pattern should be stored in $1,$2...$n???? U said it is matched but not stored in the $2 because it is not enclsoed within bracket....So what do u by that? I am new to Perl thank u ....for ur reply
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

I need to pick a part of string lets stay started with specific character and end with specific character to replace using sed command the line is like this:my audio book 71-skhdfon1dufgjhgf8.wav' I want to move the characters beginning with - end before. I have different files with random... (2 Replies)
Discussion started by: XP_2600
2 Replies

2. Shell Programming and Scripting

Regular Expressions

I am new to shell scripts.Can u please help me on this req. test_user = "Arun" if echo "test_user is a word" else echo "test_user is not a word" (1 Reply)
Discussion started by: chandrababu
1 Replies

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

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

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

regular expressions

Hello, Let say I have a string with content "Free 100%". How can extract only "100" using ksh? I would this machanism to work if instead of "100" there is any kind of combination of numbers(ex. "32", "1238", "1"). I want to get only the digits. I have written something like this: ... (4 Replies)
Discussion started by: whatever
4 Replies

7. UNIX for Dummies Questions & Answers

regular expressions

Hi Gurus, I need help with regular expressions. I want to create a regular expression which will take only alpha-numeric characters for 7 characters long and will throw out an error if longer than that. i tried various combinations but couldn't get it, please help me how to get it guys. ... (2 Replies)
Discussion started by: ragha81
2 Replies

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

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

10. Programming

regular expressions in c++

How do I use the regular expressions in c++? (2 Replies)
Discussion started by: szzz
2 Replies
Login or Register to Ask a Question