in reply to Substituting a comma only when it is preceded and followed by a bracket
One way: $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.
Re^2: Substituting a comma only when it is preceded and followed by a bracket
by barraclm (Initiate) on Nov 02, 2011 at 18:01 UTC
|
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] |
|
Bravo. Code written in this way would be instantly apparent to anyone who encountered it, and of course it would make no difference to the Perl compiler.
I am a very big believer in writing code in this way, and also in keeping a daily “running log,” or diary, in which I talk to myself about what I am doing and why. Every developer on my teams is asked to keep an ongoing project diary within the repository and to review one another’s entries from time to time. You don’t remember nearly as much detail as you suppose, not even about things that you did and even that you did recently.
Fav’d.
| |
|
|
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] |
Re^2: Substituting a comma only when it is preceded and followed by a bracket
by choroba (Cardinal) on Nov 02, 2011 at 17:28 UTC
|
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] |
|
|