Group Abstract Group Abstract

Message Boards Message Boards

0
|
5.7K Views
|
2 Replies
|
1 Total Like
View groups...
Share
Share this post:

Selectively replace strings in parentheses?

Posted 10 years ago
POSTED BY: Gregory Lypny
2 Replies
Posted 10 years ago

Hi Christopher,

Nicely done! Thank you. I did try throwing in the right parenthesis as an exception but I did not follow it with RepeatedNull. Never would have thought of that. Lots to learn.

Thanks again,

Gregory

POSTED BY: Gregory Lypny

Include ")" in an exception, to prevent overlaps.

res = StringReplace[myString,
  Shortest["(" ~~ (lhs : Except[")"] ...) ~~
     city : Except[WhitespaceCharacter] .. ~~
     ", " ~~
     st : Repeated[LetterCharacter, {2}] ~~
     sp : Repeated[WhitespaceCharacter, {1}] ~~
     zp : Repeated[DigitCharacter, {5}] ~~
     rhs___ ~~ ")"] :> city <> ", " <> st <> " " <> zp]

"Some words here, then (the quick brown fox) Los Angeles, CA 90071, and followed by Chicago, IL 60604"

myNewString === res
True
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard