Welcome Guest, Not a member yet? Register   Sign In
regex help needed
#1

(This post was last modified: 05-09-2016, 09:54 AM by cupboy.)

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    -
Reply
#2

(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?
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply
#3

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

Regular Expression online tester RegExr.

Online RegX Tester
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(This post was last modified: 05-09-2016, 11:10 PM by cupboy.)

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)
Reply




Theme © iAndrew 2016 - Forum software by © MyBB