Regexp question

  • Thread starter John.Peterson.jr
  • Start date
J

John.Peterson.jr

I have a variable like the one below. It contains nested braces pair.
I'd like to create regexp that changes the variable value to the
string "hello world". Can anyone help with that?

$test = "hello
{{ text
{{innerbrace}}
some text
{{innerbrace2}}
some text
}} world";
 
M

Mirco Wahab

I have a variable like the one below. It contains nested braces pair.
I'd like to create regexp that changes the variable value to the
string "hello world". Can anyone help with that?

$test = "hello
{{ text
{{innerbrace}}
some text
{{innerbrace2}}
some text
}} world";

Christians recommendation is meant to
follow if the things between the braces
and their nesting may mean something.

But if you only have to 'cut out the garbage',
i.e. if the braces don't mean anything, you
could do a simple:

...
if( $test =~ /([^{\r\n]+).*}([^}\r\n]+)/s ) {
print "$1$2\n";
}
...

which does retain the text parts before
and after any brace in the string (and
also chops line endings) by means of
a greedy .* and a short backtracking.

Regards


Mirco
 
D

Dr.Ruud

(e-mail address removed) schreef:
I have a variable like the one below. It contains nested braces pair.
I'd like to create regexp that changes the variable value to the
string "hello world". Can anyone help with that?

$test = "hello
{{ text
{{innerbrace}}
some text
{{innerbrace2}}
some text
}} world";

s/\{.*\}/ /s, s/\s+/ / for $test;
 
J

Jürgen Exner

I have a variable like the one below. It contains nested braces pair.
I'd like to create regexp that changes

That's not possible. REs match or don't match a string, they do not replace
anything.
Did you mean the s/// operator instead?
the variable value to the
string "hello world". Can anyone help with that?

$test = "hello
{{ text
{{innerbrace}}
some text
{{innerbrace2}}
some text
}} world";

This should do nicely
s =~ /.*/hello world/;

At least it completely fulfills your specification.
Now, if you meant something else, then maybe you could refine your
specification?

jue
 
J

joepeck02

$test =~ s/^(.*?)\{.*\}(.*)$/$1$2/s;
$test =~ s/\s+/ /sg;
Petr Vileta, Czech republic


I like Petr's solution. He uses the /s modifier which is probably the
best way to go here! It also breaks things down into 2 clear steps.
He also strictly follows the format of the text you provided. Some of
the other solutions are specific to the example you provided but may
not be nice to other strings. But if you want another cookie cutter
solution you can do it quite simply with this single regex:

$test =~ s/\s+{.*}\s+/ /s;

Replace any white space before the first {, everything until the
last }, and whitespace after the las }, all with a single space. If
you MUST use {{ and }} as the delimiters (meaning you could have a
single { or } in the text that you want to keep) you can change the
regex to this: s/\s+{{.*}}\s+/ /s;

Joe P
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top