cssfreakie Posted August 21, 2011 Share Posted August 21, 2011 1.) Why is using a Regex "un-needed overhead"? Use the right tool for the right moment. As already mentioned in this case strlen is far more efficient than using regex to accomplish exactly the same. I even saw some benchmarks to proof it. I assume when you had biology classes you weren't looking through a multimillion electromicroscoop just to see the cells better. A normal microscoop is good enough to see simple cel structures. If you like to learn regex, super! But keep in mind, anytime you ask for a regex people will respond here and say. 'why on earth do you need that, can't you use bla bla bla' because they want to help you. 3.) I prefer Regex because I can do in one line what code usually takes several likes and often isn't as thorough. Using regex, because you like the looks of it is the same non argument to use a multitrilionzillionbillion costing microscoop for highschool biology classes. Rather say I want to learn it and I can't be arsed that it's inefficient. Stating that it is 'often' less thorough is incorrect Anyway regexes are useful so learn it but don't (ab)use it for such simple tasks Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/page/2/#findComment-1260340 Share on other sites More sharing options...
doubledee Posted August 22, 2011 Author Share Posted August 22, 2011 I ran a regex on a string containing 93 characters, and then ran the same string through strlen AND trim. Here is the results. This is a string that contains more than 2 characters but less than 100 characters in length. 10,000 runs of preg_match takes 0.0234 seconds 10,000 runs of strlen() and trim() takes 0.0096 seconds. Interesting research. Thanks! So, RegEx is half as fast as your guys' way. Then again, saving an extra 1/100th of a second isn't earth-shattering to me. But you've still shown something interesting. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/page/2/#findComment-1260345 Share on other sites More sharing options...
KevinM1 Posted August 22, 2011 Share Posted August 22, 2011 Ultimately, like cssfreakie and the others have said, it's all about using the right tool for the job. When all you have is a hammer, everything starts to look like a nail. Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/page/2/#findComment-1260346 Share on other sites More sharing options...
doubledee Posted August 22, 2011 Author Share Posted August 22, 2011 1.) Why is using a Regex "un-needed overhead"? Use the right tool for the right moment. As already mentioned in this case strlen is far more efficient than using regex to accomplish exactly the same. I even saw some benchmarks to proof it. I assume when you had biology classes you weren't looking through a multimillion electromicroscoop just to see the cells better. A normal microscoop is good enough to see simple cel structures. If you like to learn regex, super! But keep in mind, anytime you ask for a regex people will respond here and say. 'why on earth do you need that, can't you use bla bla bla' because they want to help you. Okay. 3.) I prefer Regex because I can do in one line what code usually takes several likes and often isn't as thorough. Using regex, because you like the looks of it is the same non argument to use a multitrilionzillionbillion costing microscoop for highschool biology classes. Rather say I want to learn it and I can't be arsed that it's inefficient. Stating that it is 'often' less thorough is incorrect Anyway regexes are useful so learn it but don't (ab)use it for such simple tasks I didn't think I was. You guys have obviously beat that point into me! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/page/2/#findComment-1260348 Share on other sites More sharing options...
doubledee Posted August 22, 2011 Author Share Posted August 22, 2011 Ultimately, like cssfreakie and the others have said, it's all about using the right tool for the job. When all you have is a hammer, everything starts to look like a nail. That's why I am a "newbie" and you guys are PHP demigods! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/page/2/#findComment-1260351 Share on other sites More sharing options...
teynon Posted August 22, 2011 Share Posted August 22, 2011 Debbie, I didn't read all the posts... (Sorry...) But I read most of them. And I don't know if anyone answered your question on how to do it with regex. While I agree with everyone else that you shouldn't use it, this line would do what you want to do. You could tweak it for whatever. ^[\s]*([^\s][\s\S]{1,100})$ For learning regex / playing around with it, I would recommend downloading "The Regex Coach". It's good for learning and testing your regex strings and seeing what is selected, etc. http://weitz.de/regex-coach/ Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/page/2/#findComment-1260356 Share on other sites More sharing options...
doubledee Posted August 22, 2011 Author Share Posted August 22, 2011 Debbie, I didn't read all the posts... (Sorry...) But I read most of them. And I don't know if anyone answered your question on how to do it with regex. While I agree with everyone else that you shouldn't use it, this line would do what you want to do. You could tweak it for whatever. ^[\s]*([^\s][\s\S]{1,100})$ Thanks. Can you please help explain what each part does? I'm sorta stuck on its meaning. For learning regex / playing around with it, I would recommend downloading "The Regex Coach". It's good for learning and testing your regex strings and seeing what is selected, etc. http://weitz.de/regex-coach/ Good idea, but I'm a Mac person... Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/page/2/#findComment-1260358 Share on other sites More sharing options...
jcbones Posted August 22, 2011 Share Posted August 22, 2011 Then go Here Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/page/2/#findComment-1260361 Share on other sites More sharing options...
teynon Posted August 22, 2011 Share Posted August 22, 2011 ^[\s]*([^\s][\s\S]{1,100})$ Breaking it down: ^ at the beginning indicates the start of the string. $ at the end indicates the end of the string. [\s]* means any whitespace character, 0 or more. (It could have no whitespace characters or hundreds.) The () is for selecting. If you wanted to, you could use the string in a preg replacement. The [^\s] means that there must be at least one non-whitespace character. You could also do [\S] (any none whitespace character.) The ^ when used this way is similar to using ! to negate something. [\s\S] means pretty much any character {1,100} 1 or 100 times. You could further extend this statement to match more specific cases, but like everyone else said, you are making a simple task complicated. It may take a while to get a good grasp on regex. I recommend reading this page for explanation of the modifiers: http://www.php.net/manual/en/regexp.reference.escape.php and the rest of the manual in that area. I remember when I first encountered regex I had a headache for a while. Once you understand it, its not too bad. Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/page/2/#findComment-1260366 Share on other sites More sharing options...
doubledee Posted August 22, 2011 Author Share Posted August 22, 2011 ^[\s]*([^\s][\s\S]{1,100})$ If I had to guess... [\s]* zero or more whitespaces [^\s] not a whitespace why not use [\S] here instead?? [\s\S] any whitespace or non-whitespace {1,100}) Not sure what this applies to Not sure how all of these parts - when together - read?! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/page/2/#findComment-1260368 Share on other sites More sharing options...
teynon Posted August 22, 2011 Share Posted August 22, 2011 In sentence form: If the start of the string starts with any or zero whitespace characters followed by a non whitespace character (Actual input) followed by at least 1, no more than 100 characters of any type (to allow spaces) followed by the end of the string (I suppose this isn't required -> the end of the string), it is selected or returns true. You could be safer here using the word tag if you are looking for actual words. (At least 1 word, no more than 50 words.) the ^\s was more for educational purposes. (Read post above.) Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/page/2/#findComment-1260370 Share on other sites More sharing options...
doubledee Posted August 22, 2011 Author Share Posted August 22, 2011 Even though I'm not supposed to use RegEx for this problem, if we followed things through, then it should say this... "There must be at least 2 non-whitespace characters, any number of whitespace characters, and the total string cannot exceed 100 whitespace/non-whitespace characters." The example you posted earlier doesn't quite do that as far as I know. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/page/2/#findComment-1260371 Share on other sites More sharing options...
teynon Posted August 22, 2011 Share Posted August 22, 2011 I'm not really sure if it's possible to use it the way you want exactly without using at least trim. If you trim it, then you could use "^[\S][\s\S]{0,98}[\S]$". But i'm not going to promote this anymore. Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/page/2/#findComment-1260377 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.