Re: Substituting a comma only when it is preceded and followed by a bracket
by BrowserUk (Patriarch) on Nov 02, 2011 at 16:59 UTC
|
$s = 'blah blah, blah blah blah (blah, blah, blah), blah, blah';;
$s =~ s[ ( \( [^)]+ \) ) ][ ( my $x = $1 ) =~ tr[,][;]; $x ]gex;;
print $s;;
blah blah, blah blah blah (blah; blah; blah), blah, blah
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
|
|
Thank you for this response which was both fabulously quick and which worked just perfectly. Thank you also to everyone else who contributed. For the record, thankfully, I do not have to deal with nested parentheses!
So, being very greedy, but mainly because I want to learn and not have to resort to Perl Monks (too often), please could someone help me understand how the answer
s[ ( \( ^)+ \) ) ][ ( my $x = $1 ) =~ tr,;; $x ]gex;; works.
| [reply] |
|
|
s[
( ## Capture
\( ## from an open paren
[^)]+ ## all non-close paren chars
\) ## upto the close paren
)
][
## assign the captured text
## to a local variable so that it can be edited
## and then replace all ,s with ;s
( my $x = $1 ) =~ tr[,][;];
## and return the modified text to replace that captured.
$x
]
g ## globally
e ## execute the replace as code
x; ## extended notation to allow whitespace and comments
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
|
|
|
|
|
|
YAPE::Regex::Explain
Familiarity with the resources found in docs on the internet (and, in many cases, on your own machine) can help you achieve your stated goal, "...not have to resort to Perl Monks (too often)....".
So, right at your desk, perldoc -f function_name and perldoc modulename are invaluable. But, first, it'll also be worth your time to explore perldoc perldoc, (for help on how to use perldoc) and perldoc perltoc second... for the kinds of documentation available.
| [reply] [d/l] [select] |
|
|
It does not work for nested parentheses:
$s='aa, aaa, aaaa, aaa, (aaa, aaa, aaa, a), aaa, aaa, (aaa, aaa, (aaa,
+ aa), aaa, aaa), aaa, aaa';
The specification is unclear in this regard. | [reply] [d/l] |
|
|
The specification is unclear in this regard.
I think the specification is very clear and has all the appearance of being thorough. I took it to be so.
Whilst is doesn't mention that there won't be nested parens, it also doesn't mention that the commas might one of the Unicode variants (eg.(U+060C)). Either would be a significant fact worthy of mention.
For example, what would you do with commas inside the outer parens, but outside the inner ones?
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] |
Re: Substituting a comma only when it is preceded and followed by a bracket (closure)
by tye (Sage) on Nov 02, 2011 at 17:22 UTC
|
my $l= '';
$s =~ s{([(,)])}{
my $n= ',' eq $1 && '(' eq $l
? ';' : $1;
$l= $1 if ',' ne $1;
$n
}ge
| [reply] [d/l] |
|
|
Does not work for nested parentheses *ducks*
$s='aa, aaa, aaaa, aaa, (aaa, aaa, aaa, a), aaa, aaa, (aaa, aaa, (aaa,
+ aa), aaa, aaa), aaa, aaa';
| [reply] [d/l] |
|
|
| [reply] |
Re: Substituting a comma only when it is preceded and followed by a bracket
by hbm (Hermit) on Nov 02, 2011 at 18:03 UTC
|
$s = 'blah blah, blah blah blah (blah, blah, blah), blah, blah';
$s =~ s#,(?=[^(]*\))#;#g;
print $s;
blah blah, blah blah blah (blah; blah; blah), blah, blah
| [reply] [d/l] |
Re: Substituting a comma only when it is preceded and followed by a bracket
by JavaFan (Canon) on Nov 02, 2011 at 22:52 UTC
|
my $str = do {my $i; join "", map {$_ eq '(' ? $i++ : $_ eq ')' && $i
+? $i-- : $_ eq ',' && $i ? ($_ = ';') : 0; $_} split '[(,)]', $str};
This assumes the parenthesis are balanced.
But what should happen on:
"(foo),(bar)"
? The comma here is preceded and followed by a bracket - preceded by an opening paren, and followed by a closing paren even. If such a comma should be replaced as well, one might use something like (untested):
substr($str, index($str, "("), rindex($str, ")") - length($str)) =~ s/
+,/;/g;
| [reply] [d/l] [select] |
Re: Substituting a comma only when it is preceded and followed by a bracket
by Logicus (Initiate) on Nov 03, 2011 at 01:54 UTC
|
use Modern::Perl;
$_= 'blah blah, blah blah blah (blah, blah, blah), blah, blah';
while($_=~ s@\(([^\)]*?)h,@\($1h;@gs){}
say;
Output : blah blah, blah blah blah (blah; blah; blah), blah, blah
Not quite what was asked for, but it does produce the requested output :P | [reply] [d/l] |
|
|
while($_=~ s@\(([^\)]*?),@\($1;@gs){}
| [reply] [d/l] |
|
|
Oh hang on, ok there was no check to see if the comma was followed by a bracket, so if there was no close bracket, it would replace every comma to the right of the open bracket, which technically would be incorrect because the task specified there to be a following bracket and a single bracket by itself would not count. However as long as the two brackets are there it works fine.
| [reply] |
Re: Substituting a comma only when it is preceded and followed by a bracket
by Anonymous Monk on Nov 03, 2011 at 02:14 UTC
|
| [reply] |
|
|
| [reply] |