Immediately make a reservation, with the regular not so often come across

There is a value in the variable:

define ('COOKIE_PATH', 'project0');

it is necessary to change the parameter project0

My decision:

$strings =~ s/('COOKIE_PATH'\s*,\s*)'\w*'/$1'555'/g; 

The problem is that on the site of project0 can be not only letters and numbers, but also /

How to replace for example this value:

define ('COOKIE_PATH', '/ project0');

  • one
    what programming language? perl? - KoVadim
  • @KoVadim yes, perl. Initially I wanted to include it in the description of the question, but then I decided that that was not the point :) - IVsevolod
  • Regulars though similar, but in different languages ​​have their own characteristics. - KoVadim
  • khm preg_replace ('# (["\'] COOKIE_PATH [" \ '], ["\']) (?:. *?) ([" \ ']) #', '$ 1'. 'file'. ' $ 2 ', "define (' COOKIE_PATH ',' / project0 ');") With language did not guess - lampa

1 answer 1

$strings =~ s/('COOKIE_PATH'\s*,\s*)'[\w\/]*'/$1'555'/g; will go?

  • yes, thank you) it turns out I was close myself to knock about the desired solution) - IVsevolod
  • You are welcome! - VladD
  • one
    I would go further and write like this: $ strings = ~ s / ('COOKIE_PATH' \ s *, \ s *) '[^'] * '/ $ 1'555' / g; - KoVadim
  • one
    @KoVadim: it’s possible, but I like tougher parsers that don’t miss unintended characters. A matter of personal preference, of course. - VladD
  • @KoVadim and where does this love for slashes come from ( / ). $ strings = ~ s {('COOKIE_PATH' \ s *, \ s *) '[\ w /] *'} {$ 1'555 '} g; If I sclerosis does not change. - alexlz