Regex detect anything that is not between {}, and then search for what matches

I am very new to both stackoverflow and Regexes, so please forgive the errors.

I carefully searched for a regular expression to match all text that is not between the curly braces {}, and certain words are found from this text. For example, from the following line:

$content = 'Hello world, { this } is a string with { curly brackets } and this is for testing'

I would like the search for the word this return only the second occurrence of this , because it is in an area that is not inside the curly braces. Even if I can get a regular expression to match substrings behind curly braces, things will get easier for me. I found this Regex /(}([^}]*){)/ , but cannot select the Hello world, parts Hello world, and and this is for testing , because they are not inside }{ , and it selects only the part is a string with .

I would also like to ask if it is possible to combine two Regex for one purpose, such as mine. For example, the first Regex finds strings outside of {} and the second finds certain words that are searched.

I want to use this Regex in php, and now I use a function that looks more like a hack. The goal is to find specific words that are not in {} , reliably replace them and write to text files.

Thanks in advance for your help.

+6
source share
2 answers

(* SKIP) (* P)

You are lucky since php PCRE regex engine has syntax that is great for this kind of task. This neat regular expression works like a charm (see demo ):

 {[^{}]*}(*SKIP)(*F)|\bthis\b 

OK, but how does it work?

Glad you asked. Left side of alternation | corresponds to full {braces} , then deliberately fails, after which the engine moves to the next position in the line. The right side matches this words you want, and we know that they are correct because they did not match the expression on the left ...

How to use it in PHP

Just plain, something like:

 $regex = "~{[^{}]*}(*SKIP)(*F)|\bthis\b~"; $count = preg_match_all($regex,$string,$matches); 

You need to take a look at $matches[0]

Further reading about this and similar exception methods

This situation is very similar to this question about the "regex-matching pattern, if ..." , which, if you are interested and liked (*SKIP) you can read to fully understand the technique and how to expand it.

+5
source

With strings not very long, I would use simple string manipulation functions to make these searchable

 $content = 'Hello world, { this } is a string with { curly brackets } and this is for testing'; function searchify($stack,$charStart='{',$charEnd='}') { $searchArea = ''; $first = explode($charStart,$stack); foreach ($first as $string) { list($void,$ok) = (strpos($string,$charEnd) ? explode($charEnd,$string) : array('',$string)); $searchArea.= $ok; } return $searchArea; } 

this returns a cleared string, then strtr ...

 $replacing = array ('with'=>'this', "\n"=>'<br>', ' '=>"<br>",); $raw = searchify($content); $replaced = strtr($raw,$replacing); var_dump($replaced); 

... to replace the values ​​in it.

0
source

Source: https://habr.com/ru/post/970727/


All Articles