Forum Moderators: coopster

Message Too Old, No Replies

regex patterns - need a pointer

preg_match

         

Matthew1980

7:48 pm on May 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello there people of the forum!

I have finally got around to doing regex patterns on my todo list!

I'll admit to having no idea about this as I have not previously needed to use it, other than "out of the box" is the way I usually go for preg_match()

This:-

$inputData = "matthew1980";

if(preg_match("/^([a-z]|[0-9])$/i", $inputData)){
echo "Allowed";
}else{
echo "Not allowed";
}

Doesn't do what I expect it to, all I am trying to achieve is making sure that there are only numerical & alphanumerical chars in the given string - anything else throws the exception.

Thanks in advance,

Cheers,
MRb

jatar_k

7:54 pm on May 5, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hmmm

if (ctype_alnum($inputData)) {
echo "Allowed";
}else{
echo "Not allowed";
}

need regex, pshhhhh ;)

Readie

7:57 pm on May 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



([a-z]|[0-9])

Character classes here, and the pipe character are saying "A single letter OR a single number"

What you actually want here is:

/^[a-z0-9]+$/i

+ means "One or more of the preceeding character, [character class] or (parenthesized sub-expression)"

Matthew1980

8:05 pm on May 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Readie & Jatar_K,

Well I have tried both methods, and both work! Which is better than I was doing half an hour ago!

If this is considered a simple expression to do, I am not looking forward to the more 'delicate' ones...

Cheers,
MRb

Readie

8:08 pm on May 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, your original could have worked with the addition of one extra character:

/^([a-z]|[0-9])[b]+[/b]$/i

So you were close :)

rocknbil

1:28 am on May 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



need regex, pshhhhh ;)


LOL . . jatar_k has a point. :-) Being the PHP forum, the PHP stuff is what you should use for simple groups of characters.

the more 'delicate' ones...


Regexps are still "the right tool for the job" when you want to match complex patterns. I often do the same, revert to regexps when there are more simple ways to do it, but only because I have so much time invested in regexps. I still struggle with them, which is why I study the regexp puzzles posted on these forums.

Matthew1980

10:16 am on May 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Rocknbil,

Thanks for the advice - like you in a previous post, I never even knew that the funtion: ctype_alnum() existed!

But I prefer to "Right tools for the job", but obviously if there is something already there that negates the need for creating your own function using preg_match or whatever then I will do that.

I still need to delve into the world of regexp and pattern construction, as I think it will be handy in handling textarea filtration. Plus as I am now actively learning class structure I am looking at all sorts of ways of doing things :)

Cheers,
MRb

dreamcatcher

2:03 pm on May 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep, the ctype functions and character classes are very useful.

Ctype:
[php.net...]

Character classes:
[google.co.uk...]

dc