RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find Pattern Title
Expression
^([\s\S]){1,20}([\s\.])
Description
This pattern returns as much of the first x characters of a string as full words or sentences as a match, where x is currently 20. Change x to adjust the length supported in your database field. I'm currently using this within a string shortening utility to shrink large text regions to word/sentance-boundary elements and appending an ellipsis as a text continuator.
Matches
Any text of any length | ...but will only 'match' the first 20 characters at a period or space.
Non-Matches
N/A
Author Rating: The rating for this expression. Shawn Hall
Source http://ReliableAnswers.com/
Your Rating
Bad Good

Enter New Comment

Title
 
Name
 
Comment
 
Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: Using with PHP
Name: wamuonfraud
Date: 3/12/2006 9:07:21 PM
Comment:
Just what I was looking for. To use with PHP preg_match: $maxlen = 500; $regex = '%^([\s\S]){1,' . $maxlen . '}([\s\.\?!])%i'; $num_matches = preg_match($regex, $string, $_EXCERPT); $excerpt = $_EXCERPT[0];


Title: Mr
Name: Woody
Date: 11/27/2005 5:34:11 PM
Comment:
Stuart's error is caused by not having delimiters around the regex, as required by PHP in functions like preg_match(). Just do this: $pattern = '/^([\s\S]){1,20}([\s\.])/'; Notice the slashes at the beginning and end of the regex. Use $pattern in your preg_match or other PHP statement.


Title: Stuart's PHP error
Name: Shawn K. Hall
Date: 7/17/2004 6:47:18 PM
Comment:
Sorry to hear about that, but it sounds like user error to me. I've never seen the error you describe in RegEx in PHP. PHP doesn't use all the same named entities anyway, so you'd have to use a regex like the one below in order to get similar functionality: ^(.{1,50})([ \f\n\r\t\v\.])


Title: Mr
Name: Stuart Wright
Date: 7/17/2004 3:46:13 PM
Comment:
The above doesn't work in PHP. Get an error : No ending delimiter '^' found


Copyright © 2001-2024, RegexAdvice.com | ASP.NET Tutorials