Grep find multiple instances of string in same line?

511 views
Skip to first unread message

David Brostoff

unread,
May 19, 2016, 2:05:19 PM5/19/16
to textwr...@googlegroups.com
Easy, I think (although not for me):

How can I find lines with more than one instance of "@" or ".com"?

Thank you,

David

Christopher Stone

unread,
May 19, 2016, 3:03:09 PM5/19/16
to TextWrangler-Talk
On May 19, 2016, at 12:46, David Brostoff <dav...@earthlink.net> wrote:
> How can I find lines with more than one instance of "@" or ".com"?
______________________________________________________________________

Hey David,

Simple:

^.*@.*@.*

A little more complicated (reusing the first found “@” via a capture).

^.*(@).*\1.*

Simple:

^.*\.com.*\.com.*

A little more complicated (reusing the first found “\.com” via a capture).

^.*(\.com).*\1.*

--
Best Regards,
Chris

David Brostoff

unread,
May 19, 2016, 3:26:14 PM5/19/16
to textwr...@googlegroups.com
Thank you very much--these work great.

By the way, what does the "^" represent?

In the tutorial I found, it says, "beginning of line."

David

David Brostoff

unread,
May 19, 2016, 3:40:13 PM5/19/16
to textwr...@googlegroups.com
On May 19, 2016, at 12:09 PM, David Brostoff <dav...@earthlink.net> wrote:
>
> By the way, what does the "^" represent?
>
> In the tutorial I found, it says, "beginning of line."

I meant to include the URL for the tutorial:
<https://www.anybrowser.org/bbedit/grep.html#star1>

Maybe there is a better one?

David

Christopher Stone

unread,
May 19, 2016, 4:36:28 PM5/19/16
to TextWrangler-Talk
On May 19, 2016, at 14:09, David Brostoff <dav...@earthlink.net> wrote:
> By the way, what does the "^" represent?
>
> In the tutorial I found, it says, "beginning of line."
______________________________________________________________________

Hey David,

That's the caret character, and you're exactly right.

Here's my regex cheat sheet for BBEdit and TextWrangler:

https://gist.github.com/ccstone/5385334

--
Best Regards,
Chris

Patrick Woolsey

unread,
May 19, 2016, 4:59:09 PM5/19/16
to textwr...@googlegroups.com
On May 19, 2016, at 12:46, David Brostoff <dav...@earthlink.net> wrote:

>How can I find lines with more than one instance of "@" or ".com"?


To match such lines separately, please see the patterns in Chris
Stone's prior post.

To find both types of line concurrently, please try this:

^.*(@|\.com).*\1.*$



[PS: I recommend always including an end-of-line specifier $ in
patterns which one intends to make line-based matches.]


Regards,

Patrick Woolsey
==
Bare Bones Software, Inc. <http://www.barebones.com/>

Patrick Woolsey

unread,
May 19, 2016, 5:05:30 PM5/19/16
to textwr...@googlegroups.com
On 5/19/16 at 3:32 PM, dav...@earthlink.net (David Brostoff) wrote:

[...]
>I meant to include the URL for the tutorial:
><https://www.anybrowser.org/bbedit/grep.html#star1>
>
>Maybe there is a better one?

Since the above page is very old and no longer entirely
accurate, I recommend you instead refer to Chapter 8 "Searching
with Grep" in the included PDF manual (just choose Help -> User
Manual), and/or you may also find Chris's cheat-sheet helpful. :-)

Christopher Stone

unread,
May 19, 2016, 5:57:11 PM5/19/16
to TextWrangler-Talk
On May 19, 2016, at 16:05, Patrick Woolsey <pwoo...@barebones.com> wrote:
and/or you may also find Chris's cheat-sheet helpful.
______________________________________________________________________

One of the reasons I wrote the cheat-sheet is so I could open it in BBEdit (or TextWrangler) and search it.  :)

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2008/01/05 05:34
# dMod: 2016/05/19 16:50
# Appl: TextWrangler
# Task: Open BBEdit/TextWrangler RegEx Cheat Sheet
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @TextWrangler, @RegEx, @Cheat_Sheet
-------------------------------------------------------------------------------------------

set preferredWindowBounds to {451, 45, 1493, 1196}
set bbeditCheatSheetPath to "~/Documents/BBEdit Documents/Documentation/RegEx Cheat Sheet.txt"

tell application "System Events"
  set bbeditCheatSheetPath to POSIX path of disk item bbeditCheatSheetPath
end tell

tell application "TextWrangler"
  set twApp to a reference to it
  tell document "RegEx Cheat Sheet.txt"
    if it exists then
      if index of its window ≠ 1 then
        set index of its window to 1
      end if
    else
      tell twApp to open bbeditCheatSheetPath opening in new_window
    end if
    if bounds of its windowpreferredWindowBounds then
      set bounds of its window to preferredWindowBounds
    end if
  end tell
end tell

-------------------------------------------------------------------------------------------

I have this AppleScript bound to Cmd-Opt-Ctrl-G for quick access.

Of course the cheat-sheet must exist and reside at the path given in bbeditCheatSheetPath.

preferredWindowBounds is my preference, but you can easily change it to suit.

--
Best Regards,
Chris

Reply all
Reply to author
Forward
0 new messages