Regexp question

Discussion in 'Perl Misc' started by John.Peterson.jr@gmail.com, Apr 1, 2007.

  1. Guest

    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";
    , Apr 1, 2007
    #1
    1. Advertising

  2. Mirco Wahab Guest

    wrote:
    > 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
    Mirco Wahab, Apr 1, 2007
    #2
    1. Advertising

  3. Dr.Ruud Guest

    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;

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Apr 1, 2007
    #3
  4. wrote:
    > 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ürgen Exner, Apr 1, 2007
    #4
  5. Guest

    > $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
    , Apr 3, 2007
    #5
  6. <> wrote:
    >> $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



    The /s modifier is a no-op in the 2nd s/// operator above.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 3, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Hurrell
    Replies:
    4
    Views:
    152
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    466
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    342
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    185
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    170
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page