SubscribeBEGIN { regex = "{[^{]*}" }
match($0, regex) {
substring1 = substr($0, RSTART, RLENGTH);
gsub(/\ /, "_", substring1);
gsub(/{|}/, "", substring1);
sub(regex, substring1, $0);
print $0;
}Then:while (s/({[^}]*) /$1_/g) {};#!/usr/bin/perl
# Usage: blah.pl blah.txt
sub foo {
$t = shift;
$t =~ s/\s/_/g;
return $t;
}
while ( $line = <> ) {
$line =~ s/\{([^\}]+)\}/foo($1)/ge;
print $line;
}
>$ cat blah.txt
blah blah blah {blah blah blah} blah
blah {blah blah} blah {blah blah blah} blah
{bonk bonk} bonk {on the head} {bonk bonk}
$ ./blah.pl blah.txt
blah blah blah blah_blah_blah blah
blah blah_blah blah blah_blah_blah blah
bonk_bonk bonk on_the_head bonk_bonk
$str = 'blah blah blah {blah blah blah} blah';
$str =~ s!\{([^}]+)\}!$x=$1;$x=~s| |_|g;$x!ge;
print $str;doesn't do nesting, and isn't technically "one regex" but it's one line, which has to count for something.$str = 'blah blah blah {blah blah blah} blah';
$str =~ s/
\{([^}]+)\} # replace:
# opening curly bracket, string of at least
# one non-curly, closing curly bracket
/
# inline code does space-to-underscore
# replacements on the match:
$x=$1;
$x=~s| |_|g;
$x; # last line of inline code is $x, so that's the output
/xge; # x allows the comments and spaces, g is global
# and e is "execute RHS";$ perl -pe 'while (s/{([^}]*) ([^}]*)}|{([^ }]*)}/$3 || "{$1_$2}"/ge) {};'
blah {blah blah blah} blah blah {blah {blah blah} blah} blah blah
blah blah_blah_blah blah blah blah_blah_blah_blah blah blahYou are not logged in, either login or create an account to post comments
You'll probably be able to do this in a single line if you're able to process the search results with a bit of code (with the 'e' flag in Perl, for example).
posted by Khalad at 12:09 PM on January 21, 2007