0

hi looking for a regex solution to find a sentence/group of words in running text. Sentence sometimes be in a single line or wrapped into two lines. Please help.

When run against this text, there's a match:

The case has been closed and a report has been created.

When run against this text, however, it fails to match:

The case has been
closed and a report
has been created

I tried the following:

$t_pattern = "case has been closed";
preg_match($t_pattern, $var['subject'], $regs)

Basically, I'm searching in body of an email for the sentence to close a ticket. The response is sent from a support ticket system. Although it works when the entire sentence is one line like example 1, it fails in cases similar to example 2.

J. Steen
  • 15,470
  • 15
  • 56
  • 63
mishka
  • 677
  • 6
  • 20

2 Answers2

1

There's no reason to use regex for searching that particular string.

Just use a regex replace to normalize linebreaks (and other whitespaces), and then use simple substring search:

$t_pattern = "case has been closed";
$subject = preg_replace('/\s+/', " ", $var['subject']);
strpos($subject, $t_pattern)
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • preg_replace('/\s+/g', " ", $var['subject']); is returning empty string – mishka Jan 30 '13 at 16:44
  • @Manju: I'm no PHP pro, it seem like it didn't like the *global* flag. See [these](http://stackoverflow.com/q/1703320/1048572) [questions](http://stackoverflow.com/q/10810211/1048572) – Bergi Jan 30 '13 at 17:32
0

You should be okay with

$t_pattern = "/case\s+has\s+been\s+closed/";

The \s+ matches one or more whitespace characters, including newlines.

MikeM
  • 13,156
  • 2
  • 34
  • 47