Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to detect everything not between {} and then search from whats matched

Tags:

regex

php

I am very new to both stackoverflow and Regexes so please forgive mistakes.

I have been searching thoroughly for a Regex to match all text that is not between curly brackets {} and from that text find certain words. For example from the following string:

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

I would like the search for word this to return only the second occurrence of this because its in the area which is not inside curly brackets. Even if I can get a Regex to match the substrings outside the curly brackets, things get simplified for me. I found this Regex /(}([^}]*){)/ but it cannot select the parts Hello world, and and this is for testing because these are not inside }{ and it only selects is a string with part.

Also I would like to ask if it is possible to combine two Regex for a single purpose like mine. For example the first Regex finds strings outside {} and second finds specific words that are searched for.

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

Thanks in advance for your help.

like image 377
Shark Avatar asked Jun 12 '14 05:06

Shark


People also ask

How do you match everything except with regex?

How do you ignore something in regex? To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself.

What does regex (? S match?

Therefore, the regular expression \s matches a single whitespace character, while \s+ will match one or more whitespace characters.

What is the correct regex in Python method used to search for all occurrences that match a given pattern?

findall() module is used to search for “all” occurrences that match a given pattern.

How do I use regex to match?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).


2 Answers

(*SKIP)(*F)

You're in luck, as php's PCRE regex engine has a syntax that is wonderful for this kind of task. This tidy regex work like a charm (see demo):

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

Okay, but how does it work?

Glad you asked. The left side of the alternation | matches complete {braces}then deliberately fails, after which the engine skips to the next position in the string. The right side matches the this words you want, and we know they're the right ones because they weren't matched by the expression on the left...

How to use it in PHP

Just the usual, something like:

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

You'll want to have a look at $matches[0]

Further reading about this and similar exclusion techniques

This situation is very similar to this question about "regex-matching a pattern unless...", which, if you're interested and enjoyed (*SKIP) power, you might like to read to fully understand the technique and how it can be extended.

like image 129
zx81 Avatar answered Oct 07 '22 15:10

zx81


With strings not very long, I'd 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 values in it.

like image 27
Félix Adriyel Gagnon-Grenier Avatar answered Oct 07 '22 15:10

Félix Adriyel Gagnon-Grenier