Jump to content

Check if start of new line is not {


hlstriker

Recommended Posts

I'm trying to check to see if the start of a new line isn't a { bracket.

 

Here is the file layout I'm searching:

{

Bracket line 1

}

{

Bracket line 2

}

{

Bracket line 3

}

This line is not in brackets

 

And here is the pattern I'm using:

preg_match("~{\n\".*(?=}\n[^{])~msU", $string, $match);

 

So, I'm trying to get all the lines in brackets until there is not a new starting bracket. For some reason the [^{] part of the pattern seems to be the problem.

Link to comment
https://forums.phpfreaks.com/topic/128933-check-if-start-of-new-line-is-not/
Share on other sites

don't have to be that complicated, here's one without the much regexp. split the file on closing braces. Check each value and display them if there is opening braces.

$file="file";
$data = file_get_contents($file);
$data = split("}",$data); 
foreach ($data as $k=>$v){
if( strpos($v,"{") !== FALSE){
    echo "$v\n";
}
}

output:

# ./test.php
{
Bracket line 1


{
Bracket line 2


{
Bracket line 3


tested only on your supplied data.

What about this?

 

$string = <<<html
{
Bracket line 1
}
{
Bracket line 2
}
{
Bracket line 3
}
This line is not in brackets
html;

preg_match_all("#{(.*?)}#is", $string, $matches);

echo "<pre>", print_r($matches[1]), "</pre>";

preg_match_all("#{(.*?)}#is", $string, $matches);

You don't need the i modifier, as you are using the wildcard as the matching criteria.

 

Alternatively, instead of using a wildcard and a lazy quantifier, you can use a negated character class:

 

preg_match_all("#{([^}]+)}#", $string, $matches);

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.