Possible with Grep?

125 views
Skip to first unread message

Jim Straus

unread,
Feb 28, 2024, 12:38:23 PM2/28/24
to bbe...@googlegroups.com
Hello all regex experts -
  I'm looking to change a word in my code to another one, but it is sometimes "Word", "word" or "WORD".  I can do this with three search and replaces.  I also know about the \u\U modifications to the group (so \u\1 to uppercase the first letter), but that doesn't really work, since I'm changing the word (not using the group).  What I think I want is something that would take just the case of the matched group and apply that to replacement text.  Or is there another way to accomplish this in one pattern?
  Thanks!
-Jim Straus

Brian Forte

unread,
Feb 28, 2024, 4:14:26 PM2/28/24
to bbe...@googlegroups.com
The below is simplistic and almost brute-force, but it appears to work.

I’m assuming ‘word’ is representative. That is, I’m assuming you are
searching for a specific string of letters where the only potential
difference between each example string is the letter case.

Then

Search for: [W|w][O|o][R|r][D|d]
Replace with: <whatever spelling case for the ‘word’ string you want>

Hope this helps.

Regards,

Brian Forte.
--
Brian Forte
<bfo...@adelaide.on.net>

Rick Gordon

unread,
Feb 28, 2024, 4:36:16 PM2/28/24
to BBEdit Talk
(?i) in the string will render it case-insensitive, as will unchecking Case sensitive at the bottom of the BBEdit Find/Replace dialog.

GP

unread,
Feb 28, 2024, 4:39:43 PM2/28/24
to BBEdit Talk
From the BBEdit Find window's grep cheat sheet, (?i) on the find grep expression will do case insensitive matches.

Then the question is what do you want the replacement's character casing to look like? 

If you want the character casing to conform to the found match's, I don't think you can do that with a grep replace pattern.

If your replacement is just a character string of a particular character casing format (e.g., MyRePlAcEmEnT) then all you have to do is put that in the "Replace:" field and in the "Find:" field Word(?i)

That will find all forms of character casing of "word" and replace it with MyRePlAcEmEnT.

Jim Straus

unread,
Feb 28, 2024, 4:43:01 PM2/28/24
to bbe...@googlegroups.com
Sorry, maybe I wasn’t clear. Yes, “word” is a place holder. Think aWord, A_WORD, a_word as possible items to be changed. I want to change them to aSomething, A_SOMETHING, a_something. Searching for “word” in a case insensitive manner is easy, and even capture it. The question is, can I apply the caseness of the captured value to a replacement value. Maybe if there was a conditional replacement that inspects the captured value, I could do it. There are lots of controls available on the search pattern, but it seems not as much on the replacement value.
Thanks!
-Jim Straus

> On Feb 28, 2024, at 3:14 PM, Brian Forte <bfo...@adelaide.on.net> wrote:
> --
> This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
> ---
> You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/20240229065709634649.4025da7c%40adelaide.on.net.

Ulrich Kapp

unread,
Feb 28, 2024, 4:50:21 PM2/28/24
to BBEdit Talk
I don't know if I understood your intention correctly, but maybe this ist what you are looking for:

First switch of case sensitive searching!

Search pattern:

(w)(ord)


Replacement pattern:

\u\1\L\2


With this, every word |Word |WORD turns into Word.


Use the "Pattern Playground" to play around with it.

Hope it helps.

Cheers!
Ulrich

Mike Pasini

unread,
Feb 28, 2024, 5:19:02 PM2/28/24
to BBEdit Talk
I did this once upon a time but had to use separate replacements for each case pattern. Here's an example you can run in BBEdit:

#!/usr/bin/env perl

@data = ( "word","Word","WORD");

$mask = "%-10s >> %s\n";

for $datum (@data) {
    printf $mask, $datum, fix($datum);
    print "\n";
}

sub fix {
local $_ = shift;
s/\b[a-z]+?\b/something/g;
s/\b[A-Z][a-z]+?\b/Something/g;
s/\b[A-Z]+?\b/SOMETHING/g;
return $_;
}

On Wednesday, February 28, 2024 at 9:38:23 AM UTC-8 Jim Straus wrote:

Neil Faiman

unread,
Feb 28, 2024, 5:40:19 PM2/28/24
to BBEdit Talk Mailing List
I’m afraid that I don’t have anything useful to contribute, but many, many years ago (1980s), I was using a programmable text editor (anybody remember TPU / LSEdit for VMS?), and implemented exactly this feature. I used it all the time; and I’ve regretted ever since that I’ve never seen it in another editor.

Cheers,
Neil Faiman

--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.

GP

unread,
Feb 29, 2024, 1:25:29 AM2/29/24
to BBEdit Talk
Thinking outside the box...

You said you already have three search and replace operations that do what you want but are looking for a one operation solution. Canonize is one way to achieve a one operation solution.

Put your existing search and replaces in a transformation file. One search and replace per line and separate the search from the replace with a tab (a real tab not a \t). Save it for subsequent use in the (Text->)Canonize… operation and bring to front the file you want to do the changes on.

I don't know what options you're using with your existing search and replace operations. The Canonize… dialog has options for Case sensitive, Match words, and Use grep. There's corresponding mode-line variables you can embed in your transformation file that will override the dialog options set.

Given your examples, a transformation file of:
aWord    aSomething
a_WORD    a_SOMETHING
a_word    a_something

with Case sensitive and, Match words options set would replace all the word forms in a source file to the desired corresponding something forms using BBEdit's Canonize…

Holger Bartel

unread,
Feb 29, 2024, 3:15:48 AM2/29/24
to BBEdit Talk
Hi, 

Alternatively to the other suggestions, you could put the three existing search & replace patterns into a text factory to run them in one go.

Best, 
Holger 

--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.

Ulrich Kapp

unread,
Feb 29, 2024, 7:38:21 AM2/29/24
to BBEdit Talk
Another Search and replacement suggestion, wich is more versatile than my first suggestion:

Search pattern:

(?P<Normal>Word)|(?P<Lower>word)|((?P<Upper1st>W)(?P<UpperRest>ORD))


Replacement pattern:

\P<Normal>\u\P<Lower>\P<Upper1st>\L\P<UpperRest>


As I said, play around with it in the Pattern Playground, you can learn a lot about BBEdits regex search capabilities. It's a mighty tool.

EuliX
Reply all
Reply to author
Forward
0 new messages