1

I have the following named capture group that works exactly as intended. It grabs the last date/time of a specific format from a string of text.

$re = "/.*(?<date>[0-9]+\\/[0-9]+\\/[0-9]+ [0-9]+:[0-9]+:[0-9]{1,2} (AM|PM))/s";

I want to capture the user ID that follows so I changed it to the following

$re = "/.*(?<date>[0-9]+\\/[0-9]+\\/[0-9]+ [0-9]+:[0-9]+:[0-9]{1,2} (AM|PM)) (?<name>\\w+)/s"; 

However when I do so it breaks both values giving the following error

Notice: Undefined index: date in Q:\XAMPP\htdocs\index.php on line 272

The Error stems from the preg_match matches array being blank. Print_r confirms the array does not contain any information once the regex is changed to the second value.

Both of these work fine in external sites as the link below to Regex101 shows

http://regex101.com/r/zO0mK0/4

Using PHP 5.5.9

So the question is, am I missing something in this regex statement that is breaking it between the external site and my internal code or does this work meaning it is 100% purely my php that is causing this issue.

    $LastDateRegex = "/.*(?<date>[0-9]+\\/[0-9]+\\/[0-9]+ [0-9]+:[0-9]+:[0-9]{1,2} (AM|PM))/s";  
    preg_match($LastDateRegex, $arr2['WorkLog'], $LastDateMatches);
    $Modsecs = (strtotime($ts) - strtotime($LastDateMatches['date']))%60;

This is an example of the code being used. As mentioned above, I know the error stems from the $LastDateMatches array being empty for the second regex example, however the code works 100% with the first so there is something between the two that causes the issue.

7
  • Can you please provide a wider snapshot of your code? I suppose the error is not in the regexp, but in the code around (e.g. using an array $array['date'] where the 'date' key isn't initialized).
    – MrTux
    Aug 15, 2014 at 15:18
  • The notice you get is that in $LastDateMatches['date'] the 'date' key isn't initialized.
    – MrTux
    Aug 15, 2014 at 15:27
  • The code works fine as is for the first regex value. The key is initialized during the pregmatch and pulls out fine later in the code... the only issue I am having is the second regex example given stops this from working. If the code works fine with the first it should continue working with the second unless I am somehow messing up the regex or there is some limitation on named capture groups
    – James
    Aug 15, 2014 at 15:37
  • Can you post the substring that begins from the last date/time and ends after the next word. Aug 15, 2014 at 15:39
  • the Regex 101 link provided has an extended example of the string I am passing to the code.
    – James
    Aug 15, 2014 at 15:40

1 Answer 1

0

You have an extra \ here: \\w+ and \\ at a few places, if that makes a difference?

Not quite sure what you want returned but runnning this regular expression, where $str is the text you have in your regex101-link,

$regex = "/.*(?<date>\d+\/\d+\/\d+ \d+:\d+:\d{1,2} (?:AM|PM)) (?<name>\w+)/s";
preg_match($regex, $str, $LastDateMatches);

Output,

Array
(
    [0] => ...
    [date] => 6/20/2014 10:04:32 PM
    [1] => 6/20/2014 10:04:32 PM
    [name] => ihugett
    [2] => ihugett
)
2
  • This dropped the AM/PM off of my string due to making it non capture group (and for some reason I still get the same issue, if I include the "(?<name>\w+)" part it makes my preg_match not work. I have a feeling that there are some issues with my server not the regex.
    – James
    Aug 15, 2014 at 16:29
  • Missed moving the ) when I edited the regex, there you go. And yes, the AM/PM catch is in a non-capturing group since you don't need that result by itself either way, you only want it together with the timestamp based on your code. My suggestion would be to restart your Apache server and see if that solves it, the code above works on my server
    – Erlesand
    Aug 15, 2014 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.