Please help me ( [a-z]+)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vinaytvijayan
    New Member
    • Feb 2008
    • 2

    Please help me ( [a-z]+)

    I am just a beginner While studying the tutorial i could find that in order to split a sentence into its corresponding words use (Here iam giving only the relevant code)

    @array = /[a-z]+/ig;
    Actually the sentence is $sentence= 'My favorite numbers are 1,3,5,57,1001';
    Output when \n is used is:--

    My
    favorite
    numbers
    are

    My doubt is why Perl is not taking it as aaaaaa etc ,bbbbbbetc, cccccccccetc that how it printed a combination of f,a,v,o,r,i,t,e ie favorite Can anyone please help me...
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by vinaytvijayan
    I am just a beginner While studying the tutorial i could find that in order to split a sentence into its corresponding words use (Here iam giving only the relevant code)

    @array = /[a-z]+/ig;
    Actually the sentence is $sentence= 'My favorite numbers are 1,3,5,57,1001';
    Output when \n is used is:--

    My
    favorite
    numbers
    are

    My doubt is why Perl is not taking it as aaaaaa etc ,bbbbbbetc, cccccccccetc that how it printed a combination of f,a,v,o,r,i,t,e ie favorite Can anyone please help me...
    The regular expresion is taking any character a through z, upper or lower case and making a match. The + is a modifier that works on the [a-z], saying to match one or more characters. That matches up to the next space because a space is not a character. When it reaches the end, it is put into the next element of the array, @array.

    It does this for the entire sentence. The numbers are not matched because they are not characters. To match them as well, you would have to use [a-z0-9]+ in the regular expression.

    I hope this helps.

    Regards,

    Jeff

    Comment

    • eWish
      Recognized Expert Contributor
      • Jul 2007
      • 973

      #3
      Mastering Regular Expressions and understanding them is a painful process. I would suggest that you look into getting "Mastering Regular Expressions" by Jeffrey Friedl. With that said my explaniation may not be the best, so here is an excerpt from an article post in the articles section.

      Originally posted by [URL=http://www.thescripts. com/forum/thread729284.ht ml]Character Classes and Special Characters
      Written by KevinADC, October 28th, 2007[/URL]
      What is a Character Class?

      Perl uses square brackets [...] in a regular expression to define a class of characters that can match in any order. If you have a character class of [abc] and a string 'cab' the first character to match will be the 'c' because the order in which you list the characters inside the square brackets is ignored.
      I hope that this helps explain the results that you are getting.

      --Kevin

      Comment

      • vinaytvijayan
        New Member
        • Feb 2008
        • 2

        #4
        Thank you for your response and your valuable time...

        Comment

        Working...