CodeIgniter Forums
regex help needed - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: regex help needed (/showthread.php?tid=65163)



regex help needed - cupboy - 05-09-2016

I've been trying to figure this out for awhile, so it must be time to ask for help. How can I get the results I want?

Code:
$subject = '\n   MC - 5-10 OVER IN A CHURCH ZONE    - 12/08/15\n    INF - 1-5 OVER IN A SCHOOL ZONE    - 12/14/15\n';

$matches = preg_split('/ ([MF].|INF|CAP) \-(.*) \- /',$subject, NULL); // also tried: preg_match, preg_match_all
// desired result: 2 string values placed into array
// 1. MC - 5-10 OVER IN A CHURCH ZONE    -
// 2. INF - 1-5 OVER IN A SCHOOL ZONE    -



RE: regex help needed - albertleao - 05-09-2016

(05-09-2016, 09:54 AM)cupboy Wrote: I've been trying to figure this out for awhile, so it must be time to ask for help. How can I get the results I want?

Code:
$subject = '\n   MC - 5-10 OVER IN A CHURCH ZONE    - 12/08/15\n    INF - 1-5 OVER IN A SCHOOL ZONE    - 12/14/15\n';

$matches = preg_split('/ ([MF].|INF|CAP) \-(.*) \- /',$subject, NULL); // also tried: preg_match, preg_match_all
// desired result: 2 string values placed into array
// 1. MC - 5-10 OVER IN A CHURCH ZONE    -
// 2. INF - 1-5 OVER IN A SCHOOL ZONE    -

is the information you're going to be getting always in this format?


RE: regex help needed - Wouter60 - 05-09-2016

In regex, to recognize a space character, use \s
To get a match for multiple space characters, use \s+


RE: regex help needed - InsiteFX - 05-09-2016

Regular Expression online tester RegExr.

Online RegX Tester


RE: regex help needed - cupboy - 05-09-2016

I figured out how to get the results. It's a two step process though. Probably is a better way. For the time being the 2nd process is a little bit like this:
Code:
 $pattern = '/([MF].|INF|CAP) \- /';
 $subject = $matches[0][0];
 $splits = preg_split($pattern,$subject,NULL,PREG_SPLIT_DELIM_CAPTURE);
Final result looks like this:
Array ( [0] => [1] => MA [2] => POSSESSION OR USE OF A CONTROLLED SUBSTANCE - 10/23/14 [3] => MA [4] => USE OR POSSESSION OF DRUG PARAPHERNALIA - )

.. everything is split up the way I want it (the dates I don't need but I can use a third process to dump those)