Building a regex to capture INT., EXT., INT./EXT., etc

330
February 04, 2017, at 11:54 AM

I'm working through a bunch of text in which I'm looking for the following strings:

  • INT.
  • EXT.
  • INT./EXT.
  • EXT./INT.

The text under analysis is, for instance,

17   INT.   BLOOM HOUSE - NIGHT                                  17
27   INT./EXT.   BLOOM HOUSE - (PRESENT) DAY                      27

Calls in php to, for instance, preg_match("/^\w.*(INT\.\/EXT\.|EXT\.\/INT\.|EXT\.|INT\.)(.*)$/", $a_line, $matches);

and variants of that don't quite handle the greediness right (or so I think, anyway), and something gets left out, usually INT./EXT. or EXT./INT. items. Any advice? Thanks!

Answer 1

True, you need to use lazy dot matching with \w.*?, but you can also optimize the pattern to shorten the alternation group like this:

/^\w.*?(INT\.(?:\/EXT\.)?|EXT\.(?:\/INT\.)?)(.*)$/

See the regex demo

Also, if you are processing the text as a whole, you will need a /m multiline modifer.

Details:

  • ^ - start of a string
  • \w - a word char
  • .*? - any 0+ chars other than line break chars as few as possible up to the first
  • (INT\.(?:\/EXT\.)?|EXT\.(?:\/INT\.)?) - Group 1 capturing either:
    • INT\.(?:\/EXT\.)? - INT. followed with optional /EXT. substring
    • | - or
    • EXT\.(?:\/INT\.)? - EXT. followed with optional /INT. substring
  • (.*) - Group 2: any 0+ chars other than line break chars up to the...
  • $ - end of string.
Rent Charter Buses Company
READ ALSO
How to write a shortcode with variable to display content from specific custom post type post?

How to write a shortcode with variable to display content from specific custom post type post?

I've only written very basic shortcodes without variables and am just not figuring out how to write one that allows someone to enter a variable to pull info from a specific post

344
php script not connecting to database

php script not connecting to database

I wanted to avoid asking this question since there are already a few threads on it, but I am still unable to find a solution for this after 5hrs and really need some help

305
Php laravel framework resuming

Php laravel framework resuming

i created my first project in laravel-- composer create-project laravel/laravel blogHow do i resume the project after maybe restarting my computer?

412
PHP pthreads: Add a new object from an “Threaded” to the pool

PHP pthreads: Add a new object from an “Threaded” to the pool

I'm playing something around with pthreads 31

517