0

I'm trying to extract some text in between a Request XML tag (between and tags) using this Regex:

(?<=(<Request>)).*(?=(</Request>)) 

RegexBuddy shows me that it's fine, but preg_match returns this error: "Unknown modifier R". Placing a backslash before the "/" makes it so nothing returns. Does anyone know what the problem is?

Code:

$parsedQuery = fopen("c:\\query", "r") ;
$parsed ="". 
while (!feof($parsedQuery)) {
    $parsed .= fgets($parsedQuery) ;} 
$reg = "#(?<=(<Request>)).*(?=(</Request>))#";

$match = array();
preg_match($reg, $parsed, $match);
print_r($match);

Edit: I now noticed that the file opens with an unidentified character (binary value is 3F) after the opening of each tag (the "<" character). I assume php's fgets implementation does this for security measures, could this be the problem, and is there any way to surpass it?

1 Answers1

0

The problem is that in PHP (and in a lot of langugages) a regexp is something like /pattern/modifier where modifier could be, for example, g (multiple match). If you want to use / in your regexp you have to escape them with a \ :

/(?<=(<Request>)).*(?=(<\/Request>))/

See http://www.php.net/manual/en/pcre.pattern.php for more information about patterns in PHP.

Holt
  • 36,600
  • 7
  • 92
  • 139
  • Alright, even using # as a delimiter returns nothing. I now noticed that the file opens with with garbled text, read my edit for more details. – Yonathan Amir Apr 27 '14 at 11:24
  • @user1199471 Can you put your "full" code ? From the `fopen` to the preg_match or other regexp functions you're using. – Holt Apr 27 '14 at 11:46
  • @user1199471 I don't see delimiter in your code, I've tried this : `preg_match_all('#(?<=()).*(?=())#', $test, $matches)` where `$test = "Some useless string.My first request.Some other useless stuff...\n\rMy second request\n"` and it's working (with delimiters). It would be very strange if `fgets` added `?` characters to your code... And if it was the case, you wouldn't get an error but only no match. – Holt Apr 27 '14 at 12:09