Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 27th, 2012  |  syntax: None  |  size: 1.59 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Regex issue with named captured pairs
  2. start=2011-03-10T13:00:00Z;end=2011-03-30T13:00:00Z;scheme=W3C-DTF
  3.        
  4. #^start=(?P<publishDate>.+);end=(?P<expirationDate>.+);#ix'
  5.        
  6. #^(start=(?P<publishDate>.*?);)?(end=(?P<expirationDate>.*?);)?#xi
  7.        
  8. ^(start=(?P<publishDate>.+);)?(end=(?P<expirationDate>.+));)?
  9.        
  10. /^(?=                                 # from beginning, look ahead for start
  11.        .*b                               # any character 0 or more times (backtrack to match 'start')
  12.        start=(?P<publishDate>.*?);        # put start date in publish
  13.     |  (?P<publishDate>)                # OR, put empty string publish
  14.   )
  15.   (?=                                 # from beginning, look ahead for end
  16.        .*b                               # same criteria as above ...
  17.        end=(?P<expirationDate>.*?);
  18.     |  (?P<expirationDate>)
  19.   )
  20. /ix
  21.        
  22. <?php
  23. $string = "start=2011-03-(start)10T13:00:00Z;end=2011-03-(end)30T13:00:00Z;scheme=W3C-DTF";
  24. preg_match('/(?Ji)^
  25.       (?= (?| .* end = (?P<expirationDate> .*? ); | (?P<expirationDate>)) )
  26.       (?= (?| .* start = (?P<publishDate> .*? ); | (?P<publishDate>)) )
  27.     /x', $string, $matches);
  28. echo "Published = ",$matches['publishDate'],"n";
  29. echo "Expires   = ",$matches['expirationDate'],"n";
  30. print_r($matches);
  31. ?>
  32.        
  33. Published = 2011-03-(start)10T13:00:00Z
  34. Expires   = 2011-03-(end)30T13:00:00Z
  35. Array
  36. (
  37.     [0] =>
  38.     [expirationDate] => 2011-03-(end)30T13:00:00Z
  39.     [1] => 2011-03-(end)30T13:00:00Z
  40.     [publishDate] => 2011-03-(start)10T13:00:00Z
  41.     [2] => 2011-03-(start)10T13:00:00Z
  42. )
  43.        
  44. #^start=(?P<publishDate>.*?);end=(?P<expirationDate>.*?);#ix'