j.smith1981 Posted January 5, 2012 Share Posted January 5, 2012 Hi there got a bit of a thing that's maybe not bugging me but wanted to check on something with regular expressions. I have one here: preg_match('/[a-z]/', 'this may or may not be valid with 1') This still works, but I thought logically I should be putting something like [a-z0-9] or something to make numeric values valid in a regular expression but this just allows with a-z anyways, should this be happening? I mean sorry just so I can get you to understand my problem again the above complete regular expression should logically not be permitting anything from 0-9 I thought and just anything from a-z but it's not, why is this happening? Any helps much appreciated, Jeremy. Link to comment https://forums.phpfreaks.com/topic/254404-trying-to-restrict-characters-in-regular-expression-to-just-a-z/ Share on other sites More sharing options...
dharmeshpat Posted January 5, 2012 Share Posted January 5, 2012 try this out preg_match('/^[a-zA-Z\s]+/', 'this may or may not be valid with 1') Link to comment https://forums.phpfreaks.com/topic/254404-trying-to-restrict-characters-in-regular-expression-to-just-a-z/#findComment-1304464 Share on other sites More sharing options...
Adam Posted January 5, 2012 Share Posted January 5, 2012 All that regular expression is doing is looking for a single a-z character within a string. To ensure it can only contain that, you need to add ^ and $ to the start and end of your regular expression respectively. These special characters mean the start and end of the string, otherwise you'll just be searching for a pattern somewhere in the middle of the string. Also as I said your pattern is also only looking for a single character. You need to add +, as we spoke about yesterday, after the character class: /^[a-z]+$/ Link to comment https://forums.phpfreaks.com/topic/254404-trying-to-restrict-characters-in-regular-expression-to-just-a-z/#findComment-1304465 Share on other sites More sharing options...
j.smith1981 Posted January 5, 2012 Author Share Posted January 5, 2012 Yes I know, I mean I just neglected to realise I'd need to put in the plus sign + to get it to span the whole of the input/subject. It's still allowing numeric values in though, why is this? I mean I don't mind it just thought logically it should not, here's my expression as it stands now: <?php preg_match('/[a-z]+/', 'this may or may not be valid 1') Link to comment https://forums.phpfreaks.com/topic/254404-trying-to-restrict-characters-in-regular-expression-to-just-a-z/#findComment-1304468 Share on other sites More sharing options...
dharmeshpat Posted January 5, 2012 Share Posted January 5, 2012 Try this below regex won't allow the numerice value /^[a-zA-Z\s]+/ Link to comment https://forums.phpfreaks.com/topic/254404-trying-to-restrict-characters-in-regular-expression-to-just-a-z/#findComment-1304469 Share on other sites More sharing options...
AyKay47 Posted January 5, 2012 Share Posted January 5, 2012 Yes I know, I mean I just neglected to realise I'd need to put in the plus sign + to get it to span the whole of the input/subject. It's still allowing numeric values in though, why is this? I mean I don't mind it just thought logically it should not, here's my expression as it stands now: <?php preg_match('/[a-z]+/', 'this may or may not be valid 1') did you not read adams post? preg_match('/[a-z]+/', 'this may or may not be valid 1'); this will always match as long as there is a string of at least 1 a-z value, what you need to do is add ^ to the beginning and $ to the end of your pattern, this will tell the parser, I want the string to start with an alphabetic character, only contain alphabetic characters, and end with an alphabetic character. All that regular expression is doing is looking for a single a-z character within a string. To ensure it can only contain that, you need to add ^ and $ to the start and end of your regular expression respectively. These special characters mean the start and end of the string, otherwise you'll just be searching for a pattern somewhere in the middle of the string. Also as I said your pattern is also only looking for a single character. You need to add +, as we spoke about yesterday, after the character class: /^[a-z]+$/ Edit: and if you want to match a space as well, add that into your character group. Link to comment https://forums.phpfreaks.com/topic/254404-trying-to-restrict-characters-in-regular-expression-to-just-a-z/#findComment-1304472 Share on other sites More sharing options...
Adam Posted January 5, 2012 Share Posted January 5, 2012 As I said you need to add in ^ at the start of your pattern, which will match from the very start of the string, and $ at the end which will match to the end of the string. Currently your pattern makes a match but it doesn't matter where it is, so you can still have invalid characters on the outside of the match: this may or may not be valid 1 Green and blue show the matches your pattern will make. As you don't have the start/end string boundaries, then 1 is just simply not matched but the string is classed as valid. As dharmeshpat also pointed out, in order to match your string you will need to allow spaces, which you can do either with \s, which means any type of white-space (spaces, line breaks, etc), or with a single space in the character class: /^[a-z ]+$/ Link to comment https://forums.phpfreaks.com/topic/254404-trying-to-restrict-characters-in-regular-expression-to-just-a-z/#findComment-1304473 Share on other sites More sharing options...
j.smith1981 Posted January 5, 2012 Author Share Posted January 5, 2012 You know what just hit me lol, I neglected to realise that it takes appreciation to spaces, so it only goes up to yes the first word but doesn't go any further than that. I was putting in the numeric value at the end, so anything past 'this' will be immediately ignored. Makes sense now, I therefore randomly put a number in the first part of the subject and it fails to find a value, also I am pretty tired today so probably why I didn't think about yesturdays occurence of more or less the same thing. Thank you again though much appreciated! Jeremy. Link to comment https://forums.phpfreaks.com/topic/254404-trying-to-restrict-characters-in-regular-expression-to-just-a-z/#findComment-1304480 Share on other sites More sharing options...
j.smith1981 Posted January 5, 2012 Author Share Posted January 5, 2012 There must be a way of simplifying this though: <?php preg_match('/^[a-z]+ [a-z]+ [a-z]+ [a-z]+ [a-z]+ [a-z]+ [a-z]+/', 'this may or may not be valid', $matches) print_r($matches); This works absolutely fine but I am just curious to know a better way of doing this. Any help's much appreciated and thank you so far for your help, Jeremy. Link to comment https://forums.phpfreaks.com/topic/254404-trying-to-restrict-characters-in-regular-expression-to-just-a-z/#findComment-1304533 Share on other sites More sharing options...
Adam Posted January 5, 2012 Share Posted January 5, 2012 /^[a-z ]+$/ Link to comment https://forums.phpfreaks.com/topic/254404-trying-to-restrict-characters-in-regular-expression-to-just-a-z/#findComment-1304538 Share on other sites More sharing options...
AyKay47 Posted January 5, 2012 Share Posted January 5, 2012 There must be a way of simplifying this though: <?php preg_match('/^[a-z]+ [a-z]+ [a-z]+ [a-z]+ [a-z]+ [a-z]+ [a-z]+/', 'this may or may not be valid', $matches) print_r($matches); This works absolutely fine but I am just curious to know a better way of doing this. Any help's much appreciated and thank you so far for your help, Jeremy. the correct regexp really needs to be posted a fourth time for you? Link to comment https://forums.phpfreaks.com/topic/254404-trying-to-restrict-characters-in-regular-expression-to-just-a-z/#findComment-1304544 Share on other sites More sharing options...
j.smith1981 Posted January 5, 2012 Author Share Posted January 5, 2012 /^[a-z ]+$/ No this one worked hence why I didn't reply, just neglected to click solved. Link to comment https://forums.phpfreaks.com/topic/254404-trying-to-restrict-characters-in-regular-expression-to-just-a-z/#findComment-1304546 Share on other sites More sharing options...
AyKay47 Posted January 5, 2012 Share Posted January 5, 2012 /^[a-z ]+$/ No this one worked hence why I didn't reply, just neglected to click solved. was given in the third post Link to comment https://forums.phpfreaks.com/topic/254404-trying-to-restrict-characters-in-regular-expression-to-just-a-z/#findComment-1304547 Share on other sites More sharing options...
j.smith1981 Posted January 5, 2012 Author Share Posted January 5, 2012 Yes and I obviously over looked that did not I, what is your problem anyway? Link to comment https://forums.phpfreaks.com/topic/254404-trying-to-restrict-characters-in-regular-expression-to-just-a-z/#findComment-1304548 Share on other sites More sharing options...
Adam Posted January 5, 2012 Share Posted January 5, 2012 Yes and I obviously over looked that did not I, what is your problem anyway? That's kind of the problem; you don't seem to listen to what people are saying and end up wasting their time. I appreciate regex can be quite cryptic at first, but having been told four or five times you need to include ^ and $, the latest code you posted still didn't contain both of them. Read what people have put until it properly sinks in, then check the code you're working on to see if it reflects what you've been told. Link to comment https://forums.phpfreaks.com/topic/254404-trying-to-restrict-characters-in-regular-expression-to-just-a-z/#findComment-1304553 Share on other sites More sharing options...
ManiacDan Posted January 5, 2012 Share Posted January 5, 2012 And if you don't understand something, say "I don't understand." Ignoring things that you don't understand is not how you progress as a programmer (or a human being for that matter). If someone tells you something will work, try it. If it doesn't work, say so. If it works and you don't know why, ask. What happened in this thread is that you asked a question. You were given the right answer right away. You then repeated your question, showing a slightly modified piece of code that didn't incorporate the most important part of the answer you were given. The answer was repeated by three people. You followed up with another repetition of your same question, a short exposition on your own thought process, and a third version of your broken code which still didn't include the important bits. Finally, someone got frustrated and was mean about it, which caused you to acknowledge the correct solution. What you're doing is encouraging people to be rude to you so that you'll be forced to actually read what they wrote. Also, don't report someone's post because they were mean, especially if they were mean for a damn good reason. Read the posts. Use the solutions provided. If you don't want the answer, why did you ask the question? Link to comment https://forums.phpfreaks.com/topic/254404-trying-to-restrict-characters-in-regular-expression-to-just-a-z/#findComment-1304566 Share on other sites More sharing options...
Recommended Posts