RegExp and search / replace question - removing "" when around a single word without whitespace

71 views
Skip to first unread message

Rainer Krug

unread,
Apr 18, 2024, 3:04:28 AMApr 18
to BBEdit Talk
Hi

I had a regex but can't find it anymore (I found the "Manage Pattern" only now - it always reminds me of Google...).

Could somebody give me a regex which removes the `"` around "this" word, but not "this word"?

Thanks

Rainer

Kaveh Bazargan

unread,
Apr 18, 2024, 5:40:14 AMApr 18
to bbe...@googlegroups.com
can you be more specific? What is the determining factor?
  • Exactly "this" > this
  • Only single words
pls give some examples of before and after so we can see logic. thanks. 

--
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/871a5d1f-aa12-49c5-a058-cec16619ea8cn%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/

Rainer Krug

unread,
Apr 18, 2024, 8:37:09 AMApr 18
to BBEdit Talk
Sure.
Should be removed: (single words without whitespace or "-")

"singleword" -> singleworls
"Hello" -> Hello
" Hello" -> Hello [whitespace after removal of " do not matter]
" Hello " -> Hello
"Hello " -> Hello

Should NOT be removed: (strings with whitespace or "-" except ), i.e. remain unchanged

"Hello World" 
"Hello big world"
"tomato-sauce"
" Hello World "

Hope this helps,

Rainer

Kerri Hicks

unread,
Apr 18, 2024, 9:19:21 AMApr 18
to bbe...@googlegroups.com
Maybe this will help?

--Kerri

Screenshot 2024-04-18 at 9.16.46 AM.png

jj

unread,
Apr 18, 2024, 10:46:17 AMApr 18
to BBEdit Talk
Building on Kerri's solution to include non-breaking spaces, tabs and linefeeds, etc.

find:
"[\s\N{U+00A0}]*([^\s\N{U+00A0}"-]+?)[\s\N{U+00A0}]*"

replace:
\1

Screenshot 2024-04-18 at 16.42.39.png

HTH

Jean Jourdain

jj

unread,
Apr 18, 2024, 11:06:24 AMApr 18
to BBEdit Talk
Oops, previous regex will flag :

"tomato-sauce" or "mayonnaise-sauce"

This one is a bit more strict:
(?<=\s)"[\s\N{U+00A0}]*([^\s\N{U+00A0}"-]+?)[\s\N{U+00A0}]*"

But it will still flag (see the space after tomato-sauce) :
"tomato-sauce " or "mayonnaise-sauce"

There might be more edge cases, so be cautious.

Jean

Rainer Krug

unread,
Apr 19, 2024, 4:08:14 AMApr 19
to BBEdit Talk
Thanks Kerry and Jean - they look great. I will try both in real-world cases soon and come back if there are major problems I can't solve (likely any which involve modifying the regex...).

The regex will be used for text files with max about 100 lines - so a visual check afterwards is possible and in any case high;y advised.

Thanks a gain

Rainer
Reply all
Reply to author
Forward
0 new messages