1

I'm trying to write a Regexp for php to scan true files, the keyword is require and the string I want is in brackets "my string" (require would be reserved) example

File1.txt

require "testing/this/out.js"
require "de/test/as.pen"
require "my_love.test"

.....

print "I require coffee in the morning" //problem
  • The requires will be at the top of the page.
  • Follow the standard require "_String_" format

File2.txt

Class Ben extends Name

....

print "My good boss always extends my deadline" //problem
  • Extends would only be follow by class name
  • Is only one word and only one result

// looping true and determining if class or if reg file by folder structure
$subject = "Code above";
$pattern = '/^require+""/i'; // Not sure of the correct pattern
preg_match($pattern, $subject, $matches);
print_r($matches);

I just want testing this out and de/test/as.pen to return in an array for the first example.

Is this possible? will there be a lot of problems with this?

Kivylius
  • 6,357
  • 11
  • 44
  • 71
  • If I understand well, you want to be sure that `require` is not inside a string? – Casimir et Hippolyte Jan 06 '14 at 20:54
  • Are you trying to build a map of the dependencies between your files by collecting the `require`, and classes extending other classes and so on ? I really don't understand what you're trying to do from the description of your problem, so I'm guessing... – Martin Jan 06 '14 at 20:57
  • @Martin Yes im trying to build a dependency system. the regex is what I dont understand. Its the best way i think that will work. – Kivylius Jan 06 '14 at 20:58
  • I would definitely use a parser and inspect the outputted AST for that. Look at http://stackoverflow.com/questions/5586358/any-decent-php-parser-written-in-php (Here's the parser he wrote : https://github.com/nikic/PHP-Parser). If you don't know what an AST is : http://en.wikipedia.org/wiki/Abstract_syntax_tree – Martin Jan 06 '14 at 21:03

3 Answers3

1

You can use this regex:

$pattern = '/^ *require +"([^"]+)"/i';
anubhava
  • 761,203
  • 64
  • 569
  • 643
1
^require (['"])([.\w /]+)\1

match the result:

preg_match('#^require (['"])([.\w /]+)\1#', $code, $match);

Explanation:

^               #  Start of string
require         #  reserved word with an space after
(['"])          #  Quotations
(               #  Capturing group
    [.\w /]+    #   Any possible characters
)               #  End of capturing group
\1              #  Same quotation

Demo

revo
  • 47,783
  • 14
  • 74
  • 117
  • Your question is very correct but its not working correctly. Check out this js Fiddle: http://jsfiddle.net/CezarisLT/zcFEm/4/ Its almost works but the require is included. `/gm` are just two global/mutlyline flags. – Kivylius Jan 06 '14 at 22:19
  • @CezarisLT You should refer to 2nd captured group. check it here http://jsfiddle.net/fcCP4/ – revo Jan 07 '14 at 08:27
0

The idea is to skip content inside quotes:

$pattern = <<<'LOD'
~
(["']) (?> [^"'\\]++ | \\{2} | \\. | (?!\1)["'] )* \1 # content inside quotes
(*SKIP)(*FAIL)  # forces this possibility to fail
|
(?>^|\s)
require \s+ " ([^"]++) "
~xs
LOD;

preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);
print_r($matches);
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125