/ code

how to cut a string with regex (the neat way)

It is fairy a common task to cut a string - ie when a teaser text should not be longer than n characters. A lot of people use substring() or substr(), depending on their favourite language. However with regex it is really easy and you do not need to cut a word:

// in php it would be:
preg_match("/(.{1,15}(\s))/", $yourString, $matches);

// or in java something like this - maybe ; ) 
Pattern myPattern = Pattern.compile((.{1,15}(\\s)));
Matcher myMatch = myPattern.matcher(yourString);

To put it in plain english:

  • get the first 15 characters (same as substring)
  • BUT - and here goes the magic: match less until you find a whitespace character and cut the rest.

So it could look like this in php:

<?php
// you could check the length of the string but actually you don't need to ...
$myString = "People seem to enjoy things more when they know a lot of other people have been left out of the pleasure.";

preg_match("/(.{1,15}(\s))/", $yourString, $matches);
echo $matches[0]." ...";

// would print:
// People seem to ...
// and with {1,27}
// People seem to enjoy things ...
?>

beer o'clock! cheers :)

how to cut a string with regex (the neat way)
Share this