Search and replace dilemma

57 views
Skip to first unread message

Andy Nickless

unread,
Apr 21, 2023, 1:47:40 PM4/21/23
to BBEdit Talk
Forgive the level 0 question, but I want to find instances in a simple text document where various figures have got up against some text, with no space between them. (For example 123Xyz).
I want to keep the figures the same, and the following text the same, but put a word space between them. (For example 123 Xyz).
Can anyone help me with this please - I'm going round in circles here!
Andy

Neil Faiman

unread,
Apr 21, 2023, 2:09:42 PM4/21/23
to BBEdit Talk Mailing List
So you want to find all occurrences of a digit immediately followed by a letter and insert a space between them?

Get the search dialog. Check the Grep box. Clear the Case sensitive box.

Find: (\d)([a-z])
Replace: \1 \2

Click the Replace All button.

Detailed explanation:
  • \d matches any digit.
  • [a-z] matches any letter.
  • \d[a-z] would match a digit immediately followed by a letter. Parenthesizing the two parts of the search string, (\d)([a-z]) does the same search, but when it finds a match, it “captures” the substrings that match the parenthesized components — I,e, the digit and the letter.
  • In the replacement string, \1 and \2 stand for the captured substrings that match the parenthesized sub patterns, i.e., the digit and the letter.
  • So the replacement string \1 \2 stands for the matched digit, a space, and the matched letter.

So if your input text is 123XyZ, (\d) matches the 3 and assigns it to \1, ([a-z]) matches the X and assigns it to \2, and the the string 3X is replaced by 3 X.

Regards,
Neil Faiman

Kevin Shay

unread,
Apr 21, 2023, 2:58:53 PM4/21/23
to bbe...@googlegroups.com
If you only want to apply this to figures of a certain length (i.e. 3 or more digits) so it would skip things like "3d" or 'B2B" or "23AndMe", you can use curly brackets to specify a number of occurrences, like:

\d{2,]  <--matches 2 or more digits
\d{3,5} <--matches 3 to 5 digits

Kevin

--
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/BD13333E-702B-45C4-817D-1C7A41F2DB40%40faiman.org.

Kaveh Bazargan

unread,
Apr 21, 2023, 3:03:03 PM4/21/23
to bbe...@googlegroups.com
Kevin, small typo:

\d{2,] should be \d{2,}

;-)



--
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/

Kevin Shay

unread,
Apr 21, 2023, 3:21:15 PM4/21/23
to bbe...@googlegroups.com
Oops, sorry. No syntax highlighting in Gmail :)

Tom Robinson

unread,
Apr 21, 2023, 6:25:08 PM4/21/23
to bbe...@googlegroups.com
Select the search pattern in the Find dialog, head up to the Edit menu and select Copy as Styled Text

\d{3,5}


Voila :]



Jonathan Ultronz

unread,
Apr 21, 2023, 6:51:04 PM4/21/23
to bbe...@googlegroups.com

This is a regular expression pattern that matches any sequence of digits that is at least 3 digits long and no more than 5 digits long. Here's a breakdown of what each component of the pattern means:

  • \d: Matches any digit (0-9).
  • {3,5}: Quantifier that specifies that the preceding pattern (in this case, \d) should match between 3 and 5 times.

So, this pattern would match any sequence of 3, 4, or 5 consecutive digits. For example, it would match "123", "4567", or "89012", but it would not match "12" or "123456”.



On Apr 21, 2023, at 3:24 PM, Tom Robinson <barefo...@gmail.com> wrote:

\d{3,5}

Andy Nickless

unread,
Apr 22, 2023, 9:30:42 AM4/22/23
to BBEdit Talk
Wonderful!
My thanks go out to all who replied - especially Neil Faiman (who was first to reply) for his detailed and very clear explanation. Just what I needed.
Grateful thanks, once again.
Andy

Reply all
Reply to author
Forward
0 new messages