Jump to content

Recommended Posts

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

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.  8)

 

 

Debbie

 

 

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!  :P

 

 

Debbie

 

 

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

 

 

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/

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

 

^[\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.

^[\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

 

 

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.)

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.