Regular Expressions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shaoen01
    New Member
    • Nov 2008
    • 1

    #1

    Regular Expressions

    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!
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Originally posted by shaoen01
    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'.

    Originally posted by shaoen01
    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:

    Code:
    C:\Users\starkm3\Documents\Programming in Java>perl
    my ($string1, $string2) = ("My organization is great!", "My organization is great!");
    $string1 =~ s/z*/s/;
    $string2 =~ s/z.*/s/;
    print $string1, "\n", $string2, "\n";
    ^D
    sMy organization is great!
    My organis

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Here was the answer to the OPs question:

      Code:
      $_ = "My organization is great!";
      s/z.*/s/;
      print;
      All he/she had to do was try the code to find out what it does.

      Comment

      Working...