Jump to content

creating regular expression

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
mutaz

mutaz

    Newbie

  • Members
  • Pip
  • 8 posts
Hello everyone,
I want to write a regular expression for all the expressions that contains at least 1 digit and at least one letter can anyone help me and tell me how to do that...
thanx in advance

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
"/([A-Za-z0-9]+)/" is for whatever combination of letters and numbers with demand of at least one. the within [] specifies what characters are allowed, the + means one or more.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
mutaz

mutaz

    Newbie

  • Members
  • Pip
  • 8 posts
but in this case (/([A-Za-z0-9]+)/) can i write any special character
in fact i want to be able to write any kind of characters but i want to force him to write at least one digit and one letter

#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
ah, then you go with "/(.+)/" as . (dot) is any char.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#5
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
If you want anything atleast one character you could also just test the string length
if(strlen($var)>=1) {

//do stuff

}
I also think this will work:
"/(.){1,}/"
1 or more.

#6
mutaz

mutaz

    Newbie

  • Members
  • Pip
  • 8 posts
thank you for help, but i'm afraid that this is not what i was looking of because may be i couldn't explain what i need
i needed something like for example : /([0-9]+[a-zA-z]+[any character is allowed even digits and letters])/
and of course i'm not forcing to be the digit in the first place and the letter in the second.
hope that i could clarified my self

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
what is the allowed characters then?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#8
mutaz

mutaz

    Newbie

  • Members
  • Pip
  • 8 posts
let me give you an example for accepted combination and not accepted combination
accepted combination like :
123db8$$#@ (contains letters and digits)
12345f6#$# (contains letters and digits)
988888m888 (contains letters and digits)
lkasdfasd6fkjlasdf&*#@ (contains letters and digits)

not accepted combination like :
adsfgtre#$%@ not accepted because doesn't contain any digit
123456675 not accepted because doesn't contain any letter
12547@!##@ not accepted because doesn't contain any letter

i hope that is clear enough...

#9
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
Ah, that is a different thing :-) that is not within my knowledge unfortunately. I hope John or someone else here with good regexp knowledge can help you there.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The easiest way would be to require two matches, one for [0-9], the other for [a-zA-Z]. Otherwise you'd need something like:
(.*[0-9].*[a-zA-Z].*)|(.*[a-zA-Z].*[0-9].*)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#11
GabryelFall

GabryelFall

    Learning Programmer

  • Members
  • PipPipPip
  • 74 posts
There is prolly an easier way to do it, but I was thinking along the lines of Panther. Within a loop you could test for [0-9], ding a var yes of no then swoop through another loop looking for [a-Z] and ding another var. Like I said, there's prolly an easier way but that is what my limited PHP knowledge brings to the table.

Edit: *ding* new page!