Sublime Forum

Regex: Select the first 10 lines

#1

hello, I want to select all first 10 lines from severel documents. I made this regex, but is not very good. Maybe someone give me a little help:

\A^.*(?:\R.*){10}

1 Like

#2

You probably don’t need the recursion: \A(?:.*\n){10}.

0 Likes

#3

doesn’t wotk

0 Likes

#4

Worked for me, but don’t copy the period.

0 Likes

#5

nope, please see this print screen:

0 Likes

#6

I didn’t try it in a project search, just the current file search. Give me a second.

0 Likes

#7

You need a capturing group: (\A(?:.*\n){10}).

`

0 Likes

#8

nope… :frowning: the same problem

0 Likes

#9
38 matches across 38 files

Don’t know what to say, but it works for me. I’d double check your search toggles and make sure regex is enabled etc. It works perfectly for me. Again, I hope you aren’t including the dot at the end. Everytime you post my example, you include the dot. it should be:

(\A(?:.*\n){10})
0 Likes

#10

please paste a print screen of your search.

0 Likes

#11

Why are you using the “whole word” option?

(and no need for the “(?:” stuff, “\A(.*\n){10}” should do just fine.)

0 Likes

#12

Whole word option? I think you are mistaken. I was just using a non-capturing group.

Anyways:

0 Likes

#13

yes, thanks, works

0 Likes

#14

I must have copied my pattern wrong the first time. \A(?:.*\n){10} worked for me as well, so no capturing group needed.

0 Likes

#15

@mlf, I see the comment wasn’t meant for me now. Sorry for the confusion. I was the one who supplied the regex, so I thought you were speaking to me.

0 Likes

#16

the best solution is this:

\A^.*(?:\R.*){10}

0 Likes

#17

I mistook \R for recursion ?R (not every regular expression engine has this \R).

But now I better understand what is being attempted.

Anyways, \A^.*(?:\R.*){10} can potentially grab 11 lines in the right scenario which wold not fit your description, so probably not best. \A(?:.*\R){10} Would grab a hard 10 lines. If you don’t want to count empty lines, then \A(?:.*\R*){10} would grab 10 lines with actual text.

So depending on what you are actually looking for either: \A(?:.*\R){10} or \A(?:.*\R*){10} would probably be best.

If a file does not have 10 lines, but you still want to grab up to the most you can, then the either of the following may be better: \A(?:.*\R){0,10} or \A(?:.*\R*){0,10}.

0 Likes

#18

\A^.*(?:\R.*){10} it selects strictly the first 10 lines.

0 Likes

#19

Nope. Proof :slight_smile:

Something like “\A(^.*\R){10}” will work, though.

0 Likes

#20

But your example shows it selecting 11 lines and not 10…

0 Likes