Regex question

19 views
Skip to first unread message

@lbutlr

unread,
Feb 12, 2021, 7:07:06 AM2/12/21
to BBEdit Talk
I seem to be having a Brain Cloud¹ moment, but I am trying to match a pattern like this:

foo+(anything)# or foo# and NOT match

foobar#
foobar+(anything)#
foob#
foob+(anything)#
etc

foo+# is invalid and cannot occur in my dataset.

Basically, the + is optional, but if it is there, I have to account for anything after the + up to the # but not account for anything after foo unless the + is there.

I started with foo\+?.*# but that obviously failed and now I'm looking at it and it's 0500 and I should've been in bed 7 hours ago.

And yes, I am going to feel real stupid.

1. 🌋

--
I want to secede, but I don't know what state I'm in. - Bart Simpson, 2012

Neil Faiman

unread,
Feb 12, 2021, 8:46:46 AM2/12/21
to BBEdit Talk Mailing List
That looks like

foo(\+.*?)?#

(Using .*? instead of .* ensures that if you have a line like "foo+bar# foo+mung#", it will match two occurrences, "foo+bar#" and "foo+mung#", rather than a single occurrence with foo followed by the string "bar# foo+mung" followed by #.

Regards,

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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/82052BB3-5773-4E7C-9E6D-6894047AE2F8%40kreme.com.

Christopher Stone

unread,
Feb 12, 2021, 10:50:14 AM2/12/21
to BBEdit-Talk
On 02/12/2021, at 06:07, @lbutlr <kre...@kreme.com> wrote:

I seem to be having a Brain Cloud¹ moment, but I am trying to match a pattern like this:

foo+(anything)# or foo# and NOT match

foobar#
foobar+(anything)#
foob#
foob+(anything)#
etc

foo+# is invalid and cannot occur in my dataset.

Basically, the + is optional, but if it is there, I have to account for anything after the + up to the # but not account for anything after foo unless the + is there.


Something like this?

foo#|foo\+.+?#


-Chris
Reply all
Reply to author
Forward
0 new messages