Grep Find and Replace Issue

92 views
Skip to first unread message

Marcus Abundis

unread,
May 8, 2024, 8:13:41 AMMay 8
to BBEdit Talk
Greetings,

I am new to BBEdit (trying it out) and I have a problem in using the standard “Find and Replace” window in BBEdit. Only the “Grep” and “Show matches” boxes checked in that window (no other boxes checked), and the situation unfolds as follows:

TARGET STRING/FIELD: “ 29.12.2023, 10:23, ”
— Target values, held in varied records, with varied fields for each record, all in one .CSV file.

FIND STRING: “ \d\d\.\d\d\.\d\d\d\d, \d\d:\d\d, ”
— This Find String works exactly as I wish, no problems.

REPLACE STRING: “;\d\d\.\d\d\.\d\d\d\d, \d\d:\d\d, ”
— This Replace String does NOT work as desired. I want to replace the leading “ ” space with a “;” while RETAINING all original digital values.

OUTPUT STRING: “;dd.dd.dddd, dd:dd, ”
— What I ACTUALLY end up with is directly above . . .

EXPECTED OUTPUT: “;29.12.2023, 10:23, ”
— What I EXPECTED to achieve is directly above.

What am I missing?! — thanks much for your help!!

Marcus

Rich Siegel

unread,
May 8, 2024, 8:18:36 AMMay 8
to BBEdit Talk
On 8 May 2024, at 6:01, Marcus Abundis wrote:

> Greetings,
>
> I am new to BBEdit (trying it out) and I have a problem in using the
> standard “Find and Replace” window in BBEdit. Only the “Grep” and “Show
> matches” boxes checked in that window (no other boxes checked), and the
> situation unfolds as follows:
>
> TARGET STRING/FIELD: “ 29.12.2023, 10:23, ”
> — Target values, held in varied records, with varied fields for each
> record, all in one .CSV file.
>
> FIND STRING: “ \d\d\.\d\d\.\d\d\d\d, \d\d:\d\d, ”
> — This Find String works exactly as I wish, no problems.
>
> REPLACE STRING: “;\d\d\.\d\d\.\d\d\d\d, \d\d:\d\d, ”
> — This Replace String does NOT work as desired. I want to replace the
> leading “ ” space with a “;” while RETAINING all original digital values.

Chapter 8 of the user manual is an excellent reference to how Grep works (in BBEdit and in general), and in particular the section on "Writing Replacement Patterns" beginning on page 212 is relevant to your interests.

To summarize: you need to specify in your original pattern which part(s) it you want to use in the replacement, by putting parentheses around them. In the replacement pattern, you can make reference to those subpatterns to construct the replacement text.

The user manual is available via the Help menu.

The Pattern Playground (on the Search menu) is handy for exploring match and replace behavior, as well.

Enjoy,

R.


--
Rich Siegel Bare Bones Software, Inc.
<sie...@barebones.com> <https://www.barebones.com/>

Someday I'll look back on all this and laugh... until they sedate me.

Kaveh Bazargan

unread,
May 8, 2024, 12:12:58 PMMay 8
to bbe...@googlegroups.com
Try:
Search: " (\d\d\.\d\d\.\d\d\d\d, \d\d:\d\d, )"
replace: "\1"

\1 replaces the first item in brackets found.

--
This is the BBEdit Talk public discussion group. If you have a feature request or believe that the application isn't working correctly, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Mastodon: <https://mastodon.social/@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/5ad4d9d9-d32c-415a-aa7c-2d3282ad0802n%40googlegroups.com.


--
Kaveh Bazargan PhD
Director
Accelerating the Communication of Research
  https://rivervalley.io/gigabyte-wins-the-alpsp-scholarly-publishing-innovation-award-using-river-valleys-publishing-technology/

Marcus Abundis

unread,
May 9, 2024, 7:13:39 PMMay 9
to BBEdit Talk
Hey Rich,

Thanks much for looking at this issue!
Okay, I closely read the material in Chapter 8 and explored the Pattern Playground, and I am pretty sure I understand what I read and saw.
I also feel I have a grasp of HOW wildcard Find and Replace commands are SUPPOSED to work.
Using a Subpattern structure, as also suggested by Kaveh, such as " (\d\d\.\d\d\.\d\d\d\d, \d\d:\d\d, )" does not seem to improve things.
What I am trying to accomplish does not seem that complicated to me, so I am really scratching my head here.
This software should have fairly stable basic functions after 30 years, no?
It looks to me like I may have encountered a straight-up BUG — in no case should a "\d" used to find a NUMERIC value produce a "d" ALPHABETIC value output.
That's just basic mixing apples and oranges, no? I really don't know what is going on here.
Can you PLEASE tell me what EXACT string(s) you think I should be using here to realize my desired output?
I would like to confirm this is an actual bug (given I am a noob to BBEdit), before I waste anyone's time with a bug report?
For your information I am using BBEdit 15.0.3 on MacOS 11.7.10, but I first encountered this problem on an earlier version of BBEdit and MacOS.

Thanks much for the help!

Marcus

Marcus Abundis

unread,
May 9, 2024, 7:13:39 PMMay 9
to BBEdit Talk
Hi Kaveh,

Thanks much for your note!
Yeah, I see how  " (\d\d\.\d\d\.\d\d\d\d, \d\d:\d\d, )" SHOULD work with  ";(\d\d\.\d\d\.\d\d\d\d, \d\d:\d\d, )" . . .
but I don't see how using a Backreference like "\1" applies to this situation—how does this produce an ";" ?!
Am I missing something?
Can you be more specific about how this applies to my situation?

Thanks!

Marcus

Neil Faiman

unread,
May 9, 2024, 7:39:18 PMMay 9
to BBEdit Talk Mailing List
Marcus,

I think that you may not realize that the backslash codes used in replacement strings are not the same as the backslash codes used in search strings.

  • In a search string, there is a very rich collection of codes to describe the things to be matched. 
  • The result of the matching operation is simply an array of matched substrings corresponding to the parenthesized sub patterns in the search string.
  • The only backslash codes in a replacement string are a backslash followed by a digit, which means to insert the corresponding element of the matched substring array into the replacement string at this point (and “\\” which means insert a single backslash). All other backslashes are ignored.

In particular, “\d, means “match a digit” in a search string, but it means “ignore the backslash and insert the letter d” in a replacement string. 

Again, the way that you insert matched text from the search string is 

  • Use parentheses in the search string to delimit the portion of the matched string that you want to “capture,”
  • Use “backslash + digit” in the replacement string to identify which captured substring you want to insert in the result.

Regards,
Neil Faiman

-- 
This is the BBEdit Talk public discussion group. If you have a feature request or believe that the application isn't working correctly, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Mastodon: <https://mastodon.social/@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.

Kaveh Bazargan

unread,
May 9, 2024, 10:39:08 PMMay 9
to bbe...@googlegroups.com
Hi Marcus

Sorry I think I made a mistake. I am not at my computer but pls try and put a semicolon before the backslash in replace. So replace is semicolon plus whatever was in brackets. 

Sent from MetroMail


Marcus Abundis

unread,
May 10, 2024, 8:33:02 AMMay 10
to bbe...@googlegroups.com
Neil,

Okay, I NOW see if I use ";&" OR ";\1" for replacement strings I can get the desired output.
I also see if I use as you suggest "backslash + digit" or ";backslash + digit" . . . I simply get THAT text repeated back with no digits at all.

But yeah, you were MOST correct in thinking I did not understand Find and Replace roles used very different command strings. I did not see that explicitly stated in the user manual.

Problem solved (at least I think so - it all still performs a bit screwy, I think . . . won't go into detail)


You received this message because you are subscribed to a topic in the Google Groups "BBEdit Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/bbedit/7mjzs8GqtRk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to bbedit+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/31F99E1C-FB7A-424B-8084-E65902EA4C96%40faiman.org.

Marcus Abundis

unread,
May 10, 2024, 8:33:18 AMMay 10
to BBEdit Talk
Yeah, thanks. I eventually figured that out.

Kaveh Bazargan

unread,
May 10, 2024, 9:16:38 AMMay 10
to bbe...@googlegroups.com
Yes simple when you know how! Sorry about error in first reply. \1 etc replace each set of brackets. You will find regex101 helpful for search pattens. Google it. 

Sent from MetroMail


Reply all
Reply to author
Forward
0 new messages