Search Just Within Provided Line Number
-
Hello, @scott-daniels, @terry-r, @coises and All,
@scott-daniels, I realize that other people have already beaten me to it !!
Here is how I understand your question :
-
For each text file in a specific folder :
-
Don(t care about the first three lines of each file
-
And looks for, at least, one occurrence of the string
2
in the fourth line of each file
-
Am I right regarding this assumption ?
In case of a positive answer, here is my solution, similar to their ones :
-
Open the Find in Files dialog (
Ctrl + Shift + F
) -
Untick all box options
-
Fill in the string
(?-s)\A(.*\R){3}.*?\K2
in the Find what: zone -
Leave the Replace with: zone
Empty
-
Enter
*.txt
in the Filters : zone -
Enter full pathname of your directory
name
in the Directory : zone -
Select the
Regular expression
search mode -
Click once on the
Find All
button -
Wait till the
Search results
window is displayed !
Best regards
guy038
-
-
I think @Coises 's answer is best here:
- Terry didn’t use
\A
to keep the search anchored to the start of file - Guy did too much (OP didn’t mention deleting the matching text, only finding it)
- Coises gets bonus points for using the possessive form
- Terry didn’t use
-
@Alan-Kilborn said in Search Just Within Provided Line Number:
Terry didn’t use \A to keep the search anchored to the start of file
Unless I’m mistaken, doesn’t each Find in Files search always start at the beginning, thus making the \A redundant. Of course keeping it in just means being consistent with regex making, but since it’s used very in-often we hardly ever see it in regexes even for opened documents.
Terry
-
@Terry-R said in Search Just Within Provided Line Number:
doesn’t each Find in Files search always start at the beginning
Yes, but nothing prevents your regex from starting a match later in the file (if one/more happens to exist). The
\A
will do this.Of course, since OP is apparently just visually inspecting results, he could ignore any matches that aren’t near the top-of-file.
-
@Alan-Kilborn said in Search Just Within Provided Line Number:
Yes, but nothing prevents your regex from starting a match later in the file (if one/more happens to exist).
Isn’t that a contradiction?
First you agree that each search in “Find in Files” commences at the start of a file. Then you suggest somehow a regex can “slip” to another possible occurrence. I don’t see how. Even if the search is repeated it will still find the same occurrence as the previous search.
Terry
PS maybe if there is some further debate we should start a new thread rather than cloud the OP’s judgement on other things.
-
@Terry-R ,
As with all regex, if it doesn’t match at the start of the search, it keeps going to the end, checking if each position matches. Your regex will match the file below, even though the fourth line doesn’t have a 2 in it anywhere – lines 2-5 fit the pattern you defined.
one two three four five has 2 six
I am not sure @Coises is right, either, because his assumes the fourth line starts with a 2, whereas the phrasing of the OP implies it might be anywhere on line 4. (And @guy038 replicated that understanding.)
(?-s)\A(.*\R){3}\K(.*2.*)
is my suggestion – I also removed the trailing\R
, otherwise a 4-line file missing final newline wouldn’t be found. -
@PeterJones said in Search Just Within Provided Line Number:
I am not sure @Coises is right, either, because his assumes the fourth line starts with a 2
Coises uses
[^\r\n]*\K2
which would start trying to match at the beginning of line 4 (from the earlier stuff in his regex). It appears to read (starting there) as "match any non-line-ending chars zero-or-more times followed by a2
– seems to fit the bill. I’m away from my PC right now, so I can’t try it, though. -
Oh, you’re right… I missed the
[^\r\n]*
when I was reading it (and didn’t try his before my post).So yes, I just confirmed @Coises’ suggestion works on my test data, too.
-
@PeterJones said in Search Just Within Provided Line Number:
Your regex will match the file below, even though the fourth line doesn’t have a 2 in it anywhere – lines 2-5 fit the pattern you defined.
Thanks, I now stand corrected. Somehow with my meagre testing on a small file I neglected to include another occurrence further down the file. Of course that shouldn’t have affected my decision as I knew the regex completes as many iterations as necessary to include the “whole file”, but there was a snafu in my solution. Yes using the
\A
is necessary to JUST look at the nth line ONLY.Thanks
TerryPS @Alan-Kilborn I DID read your info, but somehow that didn’t allow me to make the connection and Peter’s example is what it took to get me over the line. I guess today just isn’t my day, there’s always tomorrow!
-
Hello, @scott-daniels, @terry-r, @coises @alan-kilborn, @peterjones and All,
@alan-kilborn, you said :
- Guy did too much (OP didn’t mention deleting the matching text, only finding it)
I don’t understand because, in my post, I do NOT speak about deleting the
2
digit, at any moment ?
Now, about the @coises syntax
\A([^\r\n]*+\R){3}[^\r\n]*\K2
, with an atomic ( possessive ) syntax, I don’t see the bonus to use it !Indeed, it necessarily needs to get some
EOL
chars after the possible standard chars[\r\n]*
. Thus, it will grasp all standard chars, even zero, followed withEOL
char(s), in all cases !And note that I use the
(?-s)
modifier which means that the.
regex char is identical to[^\r\n\f]
, anyway !
But, I must admit that the second @coises’s solution is more accurate, if the OP needs to get the number
2
, onlyThus, we end up, with this search regex :
(?-s)\A(?:.*\R){3}.*?\K(?<!\d)2(?!\d)
Best Regards,
guy038
-
@guy038 said in Search Just Within Provided Line Number:
I don’t understand because, in my post, I do NOT speak about deleting the 2 digit, at any moment ?
Ah…you’re right. I supposed I was confused because when you said:
Leave the Replace with: zone Empty
It appeared to me like this was setting up a replace-with-nothing aka delete operation!