1

I have the following regex to capture translation strings in a laravel project:

(trans|trans_choice|Lang::get|Lang::choice|Lang::trans|Lang::transChoice|@lang|@choice)\(([\'"]([\/a-zA-Z0-9_-]+([.][^)\'"]+?)+)[\'"])(\s?.*,\s?.*)*?[\)\]];?

The regex will happily capture:

@lang('validation.invalid')
@lang('validation.invalid', ['field' => $field])

However, the moment I introduce a line break

@lang('validation.invalid', [
'field' => $field
])

or a couple in between:

@lang('validation.invalid', [
'field' => $field,

'message' => $error->getMessage()
])

It no longer targets the string

Jacobdo
  • 1,681
  • 2
  • 18
  • 38
  • 2
    Replace `(.*,\s?.*)*?` with `.*?` and prepend the pattern with `(?s)` – Wiktor Stribiżew Jun 26 '19 at 08:51
  • It captures string with optional line breaks fine, however, now it does not catch the final enclosing parentheses – Jacobdo Jun 26 '19 at 09:33
  • 1
    Yeah, that is context dependent. I suspect you want to match them at the end of a line, thus try `(?sm)(trans|trans_choice|Lang::get|Lang::choice|Lang::trans|Lang::transChoice|@lang|@choice)\(([\'"]([\/a-zA-Z0-9_-]+([.][^)\'"]+?)+)[\'"]).*?[)\]];?$`. Or `(?sm)(trans|trans_choice|Lang::get|Lang::choice|Lang::trans|Lang::transChoice|@lang|@choice)\(([\'"]([\/a-zA-Z0-9_-]+([.][^)\'"]+?)+)[\'"])(.*?)[)\]];?$(?<!\(\))`. See https://regex101.com/r/uXcC5d/1 – Wiktor Stribiżew Jun 26 '19 at 09:38

0 Answers0