What is the best way to extract a substring within a longer string?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

What is the best way to extract a substring within a longer string?

#1 Post by goodywp » 09 Mar 2018 15:58

Hi all,

I just got a new task. It is to extract a substring within the longer string. Here is the example,

"Schemes are embedded as those scheme packs as <T501-08815-0103> in 'Schemes to Sign_for_829501.doc'"

The task is to get substring T501-08815-0103

the variation is this < > can be two as <T501-08815-0103, T501-08715-0103>

so in this case the task is to get substring T501-08815-0103, T501-08715-0103

we can tailed the format to make easier to extract substring, like this case using < >, it can be any as long as it is helpful to extract the substring...

Thanks

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: What is the best way to extract a substring within a longer string?

#2 Post by Squashman » 09 Mar 2018 16:05

FOR /F command and use the DELIMS option. Use the <> as the delimiters.
You were shown how to use the TOKENS and DELIMS options in this post.

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: What is the best way to extract a substring within a longer string?

#3 Post by goodywp » 12 Mar 2018 12:12

Squashman wrote:
09 Mar 2018 16:05
FOR /F command and use the DELIMS option. Use the <> as the delimiters.
You were shown how to use the TOKENS and DELIMS options in this post.
Thank you and get what wanted:

Code: Select all

FOR /F "tokens=2,3 delims=<>" %%A IN (scheme_source.txt) DO @echo %%A>>scheme_target.txt

Post Reply