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

Please help me ( [a-z]+)

P: 2
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...
Feb 15 '08 #1
Share this Question
Share on Google+
3 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
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
Feb 15 '08 #2

eWish
Expert 100+
P: 971
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.

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
Feb 15 '08 #3

P: 2
Thank you for your response and your valuable time...
Feb 15 '08 #4

Post your reply

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