By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,178 Members | 987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,178 IT Pros & Developers. It's quick & easy.

Regular Expressions

P: 1
Hi,

I am new to Perl and stumbled onto some regular expressions and not sure if i am right in interpreting it.

If my text is "My organization is great!"

Regular express used: s/z*/s/

Returned value will be --> My organisation is great!

Regular express used: s/z.*/s/

Returned value will be --> My organisstion is great!

Am i right? Do correct me if i am incorrect, thanks!
Nov 21 '08 #1
Share this Question
Share on Google+
2 Replies


Ganon11
Expert 2.5K+
P: 3,652
Hi,

I am new to Perl and stumbled onto some regular expressions and not sure if i am right in interpreting it.

If my text is "My organization is great!"

Regular express used: s/z*/s/

Returned value will be --> My organisation is great!
Incorrect. The regex will match 0 or more z's as soon as possible. In this case, the beginning of your string is 0 or more z's (0 z's), so it replaces the beginning of the string with an 's'.

Regular express used: s/z.*/s/

Returned value will be --> My organisstion is great!
Incorrect. This regex looks for a z, followed by any number of any character, and replaces it all with 1 z. So the resultant string would be "My organis"

Take a look at this simple perl script to examine this:

Expand|Select|Wrap|Line Numbers
  1. C:\Users\starkm3\Documents\Programming in Java>perl
  2. my ($string1, $string2) = ("My organization is great!", "My organization is great!");
  3. $string1 =~ s/z*/s/;
  4. $string2 =~ s/z.*/s/;
  5. print $string1, "\n", $string2, "\n";
  6. ^D
  7. sMy organization is great!
  8. My organis
Nov 21 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
Here was the answer to the OPs question:

Expand|Select|Wrap|Line Numbers
  1. $_ = "My organization is great!";
  2. s/z.*/s/;
  3. print;
All he/she had to do was try the code to find out what it does.
Nov 21 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.