Probs with regex and nesting

G

Gyruss

Hi,

I'm having trouble modifying the regular expression /^\{.*\}$/ so that a
string with nested curly braces is deemed unmatched. E.g

{recognizing syntax} - matched
{recognizing syntax {foo} bar} - unmatched.
{recognizing syntax {foo{ foo bar}} {bar}} - unmatched.

Can this be done?

Cheers!

use warnings;
use strict;

while (<>) {
chomp;
if (/^\{.*\}$/) {
print "Matched: |$`<$&>$'|\n";
} else {
print "No match.\n";
}
}
 
G

Gunnar Hjalmarsson

Gyruss said:
I'm having trouble modifying the regular expression /^\{.*\}$/ so that a
string with nested curly braces is deemed unmatched. E.g

{recognizing syntax} - matched
{recognizing syntax {foo} bar} - unmatched.
{recognizing syntax {foo{ foo bar}} {bar}} - unmatched.

/^{[^{}]*}$/
 
B

Brian McCauley

Gyruss said:
Hi,

I'm having trouble modifying the regular expression /^\{.*\}$/ so that a
string with nested curly braces is deemed unmatched. E.g

{recognizing syntax} - matched
{recognizing syntax {foo} bar} - unmatched.
{recognizing syntax {foo{ foo bar}} {bar}} - unmatched.

Can this be done?

Can your criteria be expressed as "a string containing another '{'
character is unmatched?" (If not how are your criteria different).

/^{[^{]*}$/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top