1

In another thread

grep search

got the proper use right for the OR argument in a grep search in BBEDIT and so was wondering whether this can be expanded as following :

is it possible to have a given set of different entries with different answers in the Find field or is it just one by one ?

Is there a (grep) symbol or script available to run the following queries as a single one :

string1|string2|string3|string4

Replace : string 5

stringA|stringB|stringC|stringD

Replace : string E

string@|string#|string$|string%

Replace : string £

For now I run each of these queries individually, but if it’s possible to group them together in a single find&replace query, that would be awesome !

Toto
  • 17,001
  • 56
  • 30
  • 41

1 Answers1

0

Here is a way to do the job with Notepad++ or any tool that uses boost regex flavour.

  • Ctrl+H
  • Find what: (s1|s2|s3|s4)|(sA|sB|sC|sD)|(s@|s#|s\$|s%)
  • Replace with: (?1s5)(?2sE)(?3s£)
  • TICK Wrap around
  • SELECT Regular expression
  • Replace all

Explanation:

(s1|s2|s3|s4)       # group 1, 1 out 4 possibilities
  |                   # OR
(sA|sB|sC|sD)       # group 2, 1 out 4 possibilities
  |                   # OR
(s@|s#|s\$|s%)      # group 3, 1 out 4 possibilities

Replacement:

(?1         # if group 1 exists
  s5          # print s5
)           # endif
(?2sE)      # if group 2 exists, print SE
(?3s£)      # if group 3 exists, print $£

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

Toto
  • 17,001
  • 56
  • 30
  • 41
  • wow thanks. Will try that out and update. I already see i can do that kind of seach in Pulsar app, which is cross-platform. Will try out the changes syntax. Very powerful... – istackoverflow Apr 15 '23 at 15:03
  • @istackoverflow: You're welcome, glad it helps. If it works for you, feel free to mark the answer as accepted, [How to accept an answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Toto Apr 15 '23 at 16:05
  • ok i need to type a follow-up. If the command syntax for the search works great (it finds all the occurrences inside the brackets), the replacement command syntax does not work. It actually prints out the entire code block, i.e. it replaces the first block with "(1s5)" instead of printing just 5. So what did i do wrong ? There's no space between the code syntax blocks, i just copy pasted your answer. – istackoverflow Apr 19 '23 at 08:33
  • PS in Pulsar i don't have this 'wrap around' option. I just have Regex and case insensitive as enabled options (other options are : match case, whole word). – istackoverflow Apr 19 '23 at 08:40
  • @istackoverflow: Your editor (Pulsar) doesn't understand “conditional replacement”. Too bad :-( – Toto Apr 22 '23 at 09:28