priya
priya

Reputation: 26749

What regex pattern should I use to find two strings with similar value on successive lines

What regex pattern should I use to find two strings with a similar value on successive lines

For example, if I have something like:

(NOTE: the following values are already indented in the front)

    end
    end

how would I search for this using the vi editor?

EDIT

I would like it to match "    end" and "    end" or "  end" and "  end"

Upvotes: 3

Views: 180

Answers (2)

Conner
Conner

Reputation: 31070

Since you mentioned that the values are indented... you can use this pattern to overcome that issue:

/end\n\s*end

The \s stands for whitespace, meaning it can be either a tab or a space. The * means there can be none or infinite of the preceding character (in this case, whitespace). If you wanted to just match two spaces you could use /end\n end or /end\n\s\send. If you want to match four you could similarly type them all out or do /end\n\s\{4}end to only match 4 whitespace characters (space or tab).

I think what you'd really like to do is match identical lines. So you can do /\(^.*$\)\n\1 to accomplish that. If you want a certain number of identical lines you could do /\(^.*$\)\(\n\1\)\{15} (this example uses 15 but you can change that to any number you'd like or switch \{15} with * for any matches.

Upvotes: 1

Tomalak
Tomalak

Reputation: 338316

Pretty straightforward:

/pattern\npattern/

Where pattern is whatever you want to find. Your question is too vague for a more useful answer.

Upvotes: 2

Related Questions