FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » help with preg_match pattern
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
help with preg_match pattern [message #184747] Tue, 28 January 2014 18:53 Go to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
Yup managed to confuse myself reading the manual.

I have an numerically indexed array that consists of fragments of
a prescription.
Rarely I may have an array element in the form of:

[Alert: This Rx was initially created on 1/27/2014.]

I want to delete those array elements (probably using
array_slice), but first I need to figure out which array elements
match the pattern. The date may change and will use one or two
digit days and months.
I could just look for the first part of the element, but I want
to be prepared for the people upstream (over whom I have no
influence - different company) to add other Alerts, some of which
I will want to ignore, some of which I may want to keep.

I am certainly open to other suggestions.

bill
Re: help with preg_match pattern [message #184748 is a reply to message #184747] Tue, 28 January 2014 19:04 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/28/2014 1:53 PM, bill wrote:
> Yup managed to confuse myself reading the manual.
>
> I have an numerically indexed array that consists of fragments of a
> prescription.
> Rarely I may have an array element in the form of:
>
> [Alert: This Rx was initially created on 1/27/2014.]
>
> I want to delete those array elements (probably using array_slice), but
> first I need to figure out which array elements match the pattern. The
> date may change and will use one or two digit days and months.
> I could just look for the first part of the element, but I want to be
> prepared for the people upstream (over whom I have no influence -
> different company) to add other Alerts, some of which I will want to
> ignore, some of which I may want to keep.
>
> I am certainly open to other suggestions.
>
> bill

I'm not real good with regex's myself, but maybe something like this
will work:

^\s*\[Alert: This Rx was initially created on
[\d]{1,2}/[\d]{1,2}/[\d]{4}\.\]\s*$

Adjust as you see fit.

BTW, I find The Regex Coach (google for it - I don't have URL handy) to
be helpful in designing appropriate regex's.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: help with preg_match pattern [message #184749 is a reply to message #184747] Tue, 28 January 2014 19:42 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Tue, 28 Jan 2014 13:53:17 -0500, bill wrote:

> Yup managed to confuse myself reading the manual.
>
> I have an numerically indexed array that consists of fragments of a
> prescription.
> Rarely I may have an array element in the form of:
>
> [Alert: This Rx was initially created on 1/27/2014.]
>
> I want to delete those array elements (probably using array_slice), but
> first I need to figure out which array elements match the pattern. The
> date may change and will use one or two digit days and months.
> I could just look for the first part of the element, but I want to be
> prepared for the people upstream (over whom I have no influence -
> different company) to add other Alerts, some of which I will want to
> ignore, some of which I may want to keep.
>
> I am certainly open to other suggestions.

"/Alert: This Rx was initially created on \d{1,2}\/\d{1,2}\/\d{4}/"

If they may use 2 or 4 digit years:

"/Alert: This Rx was initially created on \d{1,2}\/\d{1,2}\/\d{2,4}/"

Or you might chance reducing the date to [\/\d]+

"/Alert: This Rx was initially created on [\d\/]+/"

If you want multiple text phrases in the form:

Alert: phrase date"

Then you need to build your regex like:

"/Alert: (phrase|phrase|phrase|phrase.....|phrase) [\d\/]+/"

But if only some phrases end in date, then the date match could become
part of the phrase:

"/Alert: (phrase [\d\/]+|phrase|phrase|phrase.....|phrase)/"

Or an optional add-on at the end:

"/Alert: (phrase|phrase|phrase|phrase.....|phrase)( [\d\/]+)?/"

Designing a regex usually needs a bit more of a specification of the
phrases you wish to capture than "something that some third party might
decide to put there at some future date", and like most forms of coding,
there's usually more than one way to do it, and it's rare for the experts
to agree on single a "best" solution.

Are you trying to match multiple potential phrases in a single regex?
Have you looked at preg_grep, which can return the members of an array
that match a particular regex. If you combine this with array_diff, then
I think (untested) you can fairly easily find all the elements which do
not match a given regex:

$tmp = preg_grep( $patt, $array );
$array = array_diff( $array, $tmp );

Then all you need is a pattern (see above)

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: I Need to search over 100 largeish text documents efficiently. What's the best approach?
Next Topic: Phonegap upload issue with PHP server
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 13:36:51 GMT 2024

Total time taken to generate the page: 0.02483 seconds