[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regular expressions

J. mp

2/11/2007 10:59:00 PM

Hi folks,
I'm burning my head because i don't understand how regular expressions
works

I just want to validade a username wher
username ->valid
user.name ->valid

everything else is invalid

let me know the reg exp to do this

--
Posted via http://www.ruby-....

17 Answers

Tim Hunter

2/11/2007 11:05:00 PM

0

J. mp wrote:
> Hi folks,
> I'm burning my head because i don't understand how regular expressions
> works
>
> I just want to validade a username wher
> username ->valid
> user.name ->valid
>
> everything else is invalid
>
> let me know the reg exp to do this
>
>
I think more details are necessary. What characters are allowed in
"username"? Just alphabetic? Alphabetic+numbers? Anything else? Is there
a minimum number of characters? A maximum? Just Latin characters?
Similarly for "user.name" Is it the same as "username" except with a
period? Does there have to be exactly four characters before the period
and four after it? Any other constraints?

To use regular expressions you must be able to precisely state what a
"match" means.

Vincent Fourmond

2/11/2007 11:08:00 PM

0

J. mp wrote:
> Hi folks,
> I'm burning my head because i don't understand how regular expressions
> works
>
> I just want to validade a username wher
> username ->valid
> user.name ->valid
>
> everything else is invalid

Just to get you started:

/[a-z]+(\.[a-z]+)?/

Vince


--
Vincent Fourmond, PhD student (not for long anymore)
http://vincent.fourmon...

J. mp

2/11/2007 11:21:00 PM

0

Vincent Fourmond wrote:
> J. mp wrote:
>> Hi folks,
>> I'm burning my head because i don't understand how regular expressions
>> works
>>
>> I just want to validade a username wher
>> username ->valid
>> user.name ->valid
>>
>> everything else is invalid
>
> Just to get you started:
>
> /[a-z]+(\.[a-z]+)?/
>
> Vince


First of all, thanks for the attention.
More details:

max size allowed is 30
min size allowed is 5

the follwoing chars are allowed :
- _ . (Slash, undescore, perdiod)

these chars are not allowed as start neither as ending char

any alphabetic char, english chars only
case insensitive
no numbers

eg
username ->invalid
user-name -> valid
_username -> invalid
user.name -> valid
user_name ->valid

basically I want allow the same pattern allowed for emails but before
the @ char :)


--
Posted via http://www.ruby-....

Tim Hunter

2/11/2007 11:56:00 PM

0

J. mp wrote:
> Vincent Fourmond wrote:
>
>> J. mp wrote:
>>
>>> Hi folks,
>>> I'm burning my head because i don't understand how regular expressions
>>> works
>>>
>>> I just want to validade a username wher
>>> username ->valid
>>> user.name ->valid
>>>
>>> everything else is invalid
>>>
>> Just to get you started:
>>
>> /[a-z]+(\.[a-z]+)?/
>>
>> Vince
>>
>
>
> First of all, thanks for the attention.
> More details:
>
> max size allowed is 30
> min size allowed is 5
>
> the follwoing chars are allowed :
> - _ . (Slash, undescore, perdiod)
>
> these chars are not allowed as start neither as ending char
>
> any alphabetic char, english chars only
> case insensitive
> no numbers
>
> eg
> .username ->invalid
> user-name -> valid
> _username -> invalid
> user.name -> valid
> user_name ->valid
>
>

/\A[[:alpha:]][-_.[:alpha:]]{3,28}[[:alpha:]]\z/

> basically I want allow the same pattern allowed for emails but before
> the @ char :)
>
>
The above regexp does not do this. Certainly you can have numbers in
your email address, for example. Basically anything is allowed before
the @. Google "regular expression email address" for extensive
discussions about this.

J. mp

2/12/2007 12:04:00 AM

0

Timothy Hunter wrote:
> J. mp wrote:
>>>> user.name ->valid
>>
>>
>>
>>
>
> /\A[[:alpha:]][-_.[:alpha:]]{3,28}[[:alpha:]]\z/
>
>> basically I want allow the same pattern allowed for emails but before
>> the @ char :)
>>
>>
> The above regexp does not do this. Certainly you can have numbers in
> your email address, for example. Basically anything is allowed before
> the @. Google "regular expression email address" for extensive
> discussions about this.

It works well.
Thanks a lot

--
Posted via http://www.ruby-....

Gavin Kistner

2/12/2007 6:06:00 AM

0

On Feb 11, 4:21 pm, "J. mp" <joaomiguel.pere...@gmail.com> wrote:
> max size allowed is 30
> min size allowed is 5
>
> the follwoing chars are allowed :
> - _ . (Slash, undescore, perdiod)
>
> these chars are not allowed as start neither as ending char

/\A[a-z][a-z.-]{3,28}[a-z]\Z/i


Translated, that says:
* start at the beginning
* find any letter
* followed by 3-28 characters that are letters, periods, or hyphens
* followed by a letter
* follwed by the
* oh, and be case insensitive, please

Note that, per your exact instructions, this allows:
u_s_e_r_n_a_m_e
u____________________________e
z._-_.z

Brian Candler

2/12/2007 9:21:00 AM

0

On Mon, Feb 12, 2007 at 08:56:12AM +0900, Timothy Hunter wrote:
> /\A[[:alpha:]][-_.[:alpha:]]{3,28}[[:alpha:]]\z/
>
> >basically I want allow the same pattern allowed for emails but before
> >the @ char :)
> >
> >
> The above regexp does not do this. Certainly you can have numbers in
> your email address, for example. Basically anything is allowed before
> the @. Google "regular expression email address" for extensive
> discussions about this.

And if you are being pedantic, RFC2822 doesn't allow E-mail addresses to
contain two dots next to each other, unless the local-part is quoted.

J. mp

2/12/2007 10:50:00 AM

0

Gavin Kistner wrote:
> On Feb 11, 4:21 pm, "J. mp" <joaomiguel.pere...@gmail.com> wrote:
>> max size allowed is 30
>> min size allowed is 5
>>
>> the follwoing chars are allowed :
>> - _ . (Slash, undescore, perdiod)
>>
>> these chars are not allowed as start neither as ending char
>
> /\A[a-z][a-z.-]{3,28}[a-z]\Z/i
>
>
> Translated, that says:
> * start at the beginning
> * find any letter
> * followed by 3-28 characters that are letters, periods, or hyphens
> * followed by a letter
> * follwed by the
> * oh, and be case insensitive, please
>
> Note that, per your exact instructions, this allows:
> u_s_e_r_n_a_m_e
> u____________________________e
> z._-_.z


Oh damm!! the first should be allowed but second and the third should
not be allowed
thnaks

--
Posted via http://www.ruby-....

J. mp

2/12/2007 11:38:00 AM

0

Brian Candler wrote:
> On Mon, Feb 12, 2007 at 08:56:12AM +0900, Timothy Hunter wrote:
>> /\A[[:alpha:]][-_.[:alpha:]]{3,28}[[:alpha:]]\z/
>>
>> >basically I want allow the same pattern allowed for emails but before
>> >the @ char :)
>> >
>> >
>> The above regexp does not do this. Certainly you can have numbers in
>> your email address, for example. Basically anything is allowed before
>> the @. Google "regular expression email address" for extensive
>> discussions about this.
>
> And if you are being pedantic, RFC2822 doesn't allow E-mail addresses to
> contain two dots next to each other, unless the local-part is quoted.

Ok, thanks all, I need a reg expr do what I described before without the
dots, slashes and underscores one after another, and not in the start
nor in the end
Thnaks

--
Posted via http://www.ruby-....

Martin DeMello

2/12/2007 11:52:00 AM

0

On 2/12/07, J. mp <joaomiguel.pereira@gmail.com> wrote:
> >
> > Note that, per your exact instructions, this allows:
> > u_s_e_r_n_a_m_e
> > u____________________________e
> > z._-_.z
>
>
> Oh damm!! the first should be allowed but second and the third should
> not be allowed

The regexp could be extended to allow this, but it gets ever more
convoluted and unreadable - you'd be better off doing a separate check
for a !~ /[^A-Za-z]{2,}/ (that is, "a does not match two
non-alphanumeric chars in a row"

>> tests = %w( u_s_e_r_n_a_m_e
u____________________________e
z._-_.z
)
=> ["u_s_e_r_n_a_m_e", "u____________________________e", "z._-_.z"]
>> tests.each {|a| p [a, a !~ /[^A-Za-z]{2,}/]}
["u_s_e_r_n_a_m_e", true]
["u____________________________e", false]
["z._-_.z", false]

Also, play around with http://weitz.de/re...

martin