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
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.

Link to comment
Share on other sites

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);

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.