I am working on a bash script that can add nginx location blocks to a file taking in a URL. To prevent duplicates this script will also remove them if if it already exists.
For removing a block if it already exists I made the regex below.
^location\s\/${URLGOESHERE} {[\s\S]*?(?=\n{2,})$
The regex needs to match the entire multiline block like this:
location /URLGOESHERE {
try_files $uri /URLGOESHERE/index.php?$query_string;
}
I would like the regex to match anything inside the block until the close bracket}
There will be multiple blocks inside the file
e.g
location /URL1HERE {
expires 7d;
try_files $uri /URLGOESHERE/index.php?$query_string;
allow all;
Etc....
}
location /URL2HERE {
try_files $uri /URLGOESHERE/index.php?$query_string;
}
location /URL3HERE {
try_files $uri /URLGOESHERE/index.php?$query_string;
}
location /URL4HERE {
expires 7d;
try_files $uri /URLGOESHERE/index.php?$query_string;
allow all;
Etc....
}
The regex I made works but only if there is a blank line before and after the block. So with my regex URL2, 3(No newline either before or after) and 4 (No newline at end of file) would not be found by pcregrep
I was wondering if the regex can be made to match the block exactly without needing these blank lines.