Bill
Bill

Reputation:

Truncate Text at a Stop Character Match

How would I truncate a sentence at a certain character:

$sentence = 'Stack Overflow - Ask Questions Here';

so that only the following is echoed:

Stack Overflow

The character count varies, but the stop point is always "Space Dash Space"

Upvotes: 1

Views: 1432

Answers (3)

Brent.Longborough
Brent.Longborough

Reputation: 9775

Assuming Perl, try this:

$sentence1 = 'Stack Overflow - Ask Questions Here - And more here';
$sentence2 = 'Just Stack Overflow';

$sentence1 =~ /^(.*?)( - |$)/;
print $1, "\n";

$sentence1 =~ /^(?|(.*) - |(.*)$)/;
print $1, "\n";

$sentence2 =~ /^(.*?)( - |$)/;
print $1, "\n";

$sentence2 =~ /^(?|(.*) - |(.*)$)/;
print $1, "\n";

These will match up to the first or the last " - ", or the whole string if there's no " - "

Upvotes: 0

Rob Cowie
Rob Cowie

Reputation: 22619

If using python a non-regexp approach would be:

>>> s = 'Stack Overflow - Ask Questions Here'  
>>> s.split(' - ')  
['Stack Overflow', 'Ask Questions Here']  
>>> # To get the substring before the match  
>>> s.split(' - ')[0]  
'Stack Overflow'

A regexp approach might be:

>>> import re
>>> re.split(' - ', s)[0]
'Stack Overflow'

Of course, you could build a regexp to match the entire string with your expected token, and group the first portion but given these two methods that is more work than is necessary.

Upvotes: 2

Robert Gamble
Robert Gamble

Reputation: 109092

Although you didn't mention a language, I am going to guess Perl due to the $variable name. In Perl one of the easiest ways to do this is using a simple regular expression:

$sentence = 'Stack Overflow - Ask Questions Here';

if ($sentence =~ /^(.*?) - /) {
  print "Found match: '$1'\n";
}

This matches the first part of the string, in a non-greedy fashion, up until the first space-dash-space sequence. The parenthesis around the first part of the expression indicates that the matching part should be "captured", in Perl it will be stored in the variable $1 (other captured patterns are stored into $2, $3, etc). If a match is found, the matching part is stored into $1 and then printed.

Upvotes: 0

Related Questions