REGEX Help - should be easy for you!

Hello, I am using this REGEX to capture the first group of characters before a period, questionmark or exclamation point. I want to broaden this to include a dash surrounded by spaces such as " - " , what say you?

Here's what I have:
/^([^.!?]*[\.!?]+){0,1}/

Open in new window



Thank you!
EffinGoodAsked:
Who is Participating?
 
ozoCommented:
function limit_sentence($text, $limit)
{
#    preg_match('/^([^.!?]*[\.!?]+){0,'.$limit.'}/', $text, $abstract);
    preg_match('/^(.*?([.!?]| - )+){0,'.$limit.'}/', $text, $abstract);
    return $abstract[0];
}
echo limit_sentence("I want to broaden this to include a dash surrounded by spaces such as ' - ' , what say you?", 1);
0
 
ozoCommented:
/^(.*?([.!?]| - )+)/
0
 
EffinGoodAuthor Commented:
Hi, that does not work at all. :(
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
ozoCommented:
In what way does it not work?
<?php
if (preg_match("/^(.*?([.!?]| - )+)/", "I want to broaden this to include a dash surrounded by spaces such as  -  , what say you?", $matches)) {
  echo "Match was found <br />";
  echo $matches[0];
}
?>
0
 
EffinGoodAuthor Commented:
Thanks for your help.

I wanted to capture the match and your regex didn't seem to capture anything. Sorry, im no good at regex.
0
 
ozoCommented:
How did you attempt to capture the match?
0
 
EffinGoodAuthor Commented:
Thanks again, here's my entire function:

function limit_sentence($text, $limit)
{
    preg_match('/^([^.!?]*[\.!?]+){0,'.$limit.'}/', $text, $abstract);
    return $abstract[0];
}

Open in new window

0
 
EffinGoodAuthor Commented:
Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.