How to exclude strings that do not begin with (insert here)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
eax22
Posts: 13
Joined: 15 Apr 2017 15:02

How to exclude strings that do not begin with (insert here)

#1 Post by eax22 » 17 Apr 2017 05:17

From this I would get all strings that are eight numbers in length, but what if I want to exclude all of the results that do not begin with two specific numbers? For example out of 12345678 23456789 34567890 I want only those that begin with 34, so the last result would be the only one to show up.

Code: Select all

findstr /m /R "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" *.txt


I thought about printing them to a file and using "34.*" but is there another way?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to exclude strings that do not begin with (insert here)

#2 Post by aGerman » 17 Apr 2017 05:41

Your explanation is somehow confusing. You're talking about strings but you used option /m.
From what I understood:

Code: Select all

set "first2digits=34"
for /f "tokens=1* delims=:" %%i in ('findstr /rb "%first2digits%[0-9][0-9][0-9][0-9][0-9][0-9]" *.txt') do echo %%j


Steffen

eax22
Posts: 13
Joined: 15 Apr 2017 15:02

Re: How to exclude strings that do not begin with (insert here)

#3 Post by eax22 » 17 Apr 2017 10:04

Apologize for any noobie mistakes, but I thought /m would print the filename containing the string(s) that I want? It's basically meant to be a program that finds all phone numbers that I have saved on the computer. They length is always the same and the first two numbers are always the same, that is why I thought about applying these two to the search.

Your code is exactly what I wanted to do, it's more elegant than the approach I initially thought about, but then again, I naturally seem to take the harder routes (beginner habit?)

Thanks 4 the help.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to exclude strings that do not begin with (insert here)

#4 Post by aGerman » 17 Apr 2017 12:14

I thought /m would print the filename containing the string(s)

That's correct. But only the file names without the found strings.
If you also want to see the file names execute only the findstr command line without the FOR loop.

Code: Select all

findstr /rb "%first2digits%[0-9][0-9][0-9][0-9][0-9][0-9]" *.txt
The output will be in that format:
File Name:Found String

Steffen

eax22
Posts: 13
Joined: 15 Apr 2017 15:02

Re: How to exclude strings that do not begin with (insert here)

#5 Post by eax22 » 17 Apr 2017 13:14

I made a text file with the content 34111111 and another one with 34111111111111111111111. Both of them came up, instead of only the one with eight numbers. Is not the limit set by the amount of times I write [0-9]? F.ex., [0-9][0-9] = 2, [0-9][0-9][0-9] = 3. A bit confused here, any ideas as to why this happens?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to exclude strings that do not begin with (insert here)

#6 Post by aGerman » 17 Apr 2017 13:32

If the line only consists of the number then replace option b with x.
If something else is following in the line then append a word boundary to the search pattern

Code: Select all

findstr /rb "%first2digits%[0-9][0-9][0-9][0-9][0-9][0-9]\>" *.txt

Steffen

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: How to exclude strings that do not begin with (insert here)

#7 Post by ShadowThief » 17 Apr 2017 13:35

Nope, you're saying "find a string that contains [0-9][0-9][0-9][0-9][0-9][0-9][0-9]", and 3411111111111111111111111 does that, it just contains a lot of other numbers.

If the phone numbers are one per line, you can say that there will be 8 digits and then the end of the line

Code: Select all

findstr /rb "%first2digits%[0-9][0-9][0-9][0-9][0-9][0-9]$" *.txt


If there are other things on the line, you can just throw a space at the end of the search string

Code: Select all

findstr /rb "%first2digits%[0-9][0-9][0-9][0-9][0-9][0-9] " *.txt

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to exclude strings that do not begin with (insert here)

#8 Post by aGerman » 17 Apr 2017 13:39

ShadowThief I assume the latter won't work without option c.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: How to exclude strings that do not begin with (insert here)

#9 Post by ShadowThief » 17 Apr 2017 14:43

It would if /C and /R played nice together. Unfortunately, what I've suggested won't work at all and they should stick to your recommendation of using \> for word barriers.

I really there was only one flavor of regex; it would make things so much easier. Just a /^([0-9]{7}).*/ and you're done.

eax22
Posts: 13
Joined: 15 Apr 2017 15:02

Re: How to exclude strings that do not begin with (insert here)

#10 Post by eax22 » 17 Apr 2017 14:57

aGerman wrote:ShadowThief I assume the latter won't work without option c.


You mean

Code: Select all

findstr /rb /c:"[3][4][0-9][0-9][0-9][0-9][0-9][0-9] " *.txt
?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to exclude strings that do not begin with (insert here)

#11 Post by aGerman » 17 Apr 2017 15:10

Yes. Or simply

Code: Select all

findstr /rbc:"%first2digits%[0-9][0-9][0-9][0-9][0-9][0-9] " *.txt

But note that this will also match the space next to the number. If the line ends or there follows something else (like a tab or hyphen) it would fail.

Steffen

Post Reply