Using JREPL to extract only the first occurrence of a certain "bracket"?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
vin97
Posts: 27
Joined: 17 Apr 2020 08:30

Using JREPL to extract only the first occurrence of a certain "bracket"?

#1 Post by vin97 » 17 Apr 2020 08:42

Hello!

I have been tinkering around with JREPL for two days now and while I got it working for 90% of the tasks I needed, there is one left that is giving me trouble.

I want to extract the content between two certain strings but only until the first occurrence of the second string.
Both strings contain the following characters btw: " : {

Example:

Code: Select all

blablabla StartTriggerString important stuff EndTriggerString blablabla EndTriggerString blablabla
Desired result:

Code: Select all

 important stuff 

siberia-man
Posts: 174
Joined: 26 Dec 2013 09:28
Contact:

Re: Using JREPL to extract only the first occurrence of a certain "bracket"?

#2 Post by siberia-man » 17 Apr 2020 15:09

Sorry for some kind of ads to my tool when you asked how to work with another one.

What about the alternative from viewtopic.php?f=3&t=9476&hilit=wsx ?

It works as the swiss watches:

Code: Select all

wsx /n /e:"m = LINE.match(/StartTriggerString(.*?)EndTriggerString/); m && print(m[1])"
for example...

This one is your test string

Code: Select all

c:\>echo blablabla StartTriggerString important stuff EndTriggerString blablabla EndTriggerString blablabla
blablabla StartTriggerString important stuff EndTriggerString blablabla EndTriggerString blablabla
And this one is the test string with the proper filter from above:

Code: Select all

c:\>echo blablabla StartTriggerString important stuff EndTriggerString blablabla EndTriggerString blablabla | wsx /n /e:"m = LINE.match(/StartTriggerString(.*?)EndTriggerString/); m && print(m[1])"
 important stuff

vin97
Posts: 27
Joined: 17 Apr 2020 08:30

Re: Using JREPL to extract only the first occurrence of a certain "bracket"?

#3 Post by vin97 » 17 Apr 2020 18:05

Thanks for the reply!

I actually found the correct JREPL syntax before the topic was approved.

Code: Select all

jrepl "StartTriggerString(.*?)EndTriggerString" "$1" /x /m /jmatch /i

dbenham
Expert
Posts: 2451
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Using JREPL to extract only the first occurrence of a certain "bracket"?

#4 Post by dbenham » 17 Apr 2020 18:12

Better to use /JMATCHQ instead of /JMATCH - It is a bit more verbose, but much faster.

Assuming your StartTriggerString and EndTriggerString are always on the same line, and you want to capture all occurrences, then you just need the vanilla ? operator to make a search non greedy (stop at the earliest point possible)

Code: Select all

call jrepl "StartTriggerString(.*?)EndTriggerString" "$txt=$1" /jmatchq /f input.txt
If you only want the first occurrence on each line:

Code: Select all

call jrepl "StartTriggerString(.*?)EndTriggerString.*" "$txt=$1" /jmatchq /f input.txt
If you want only the first occurrence in the entire file:

Code: Select all

call jrepl "StartTriggerString(.*?)EndTriggerString.*" "$txt=$1;quit=true" /jmatchq /f input.txt

If the content between the start and end triggers can span multiple lines and you want all occurrences, then add the /M option and use [\s\S] to match all characters including \r and \n:

Code: Select all

call jrepl "StartTriggerString([\s\S]*?)EndTriggerString" "$txt=$1" /m /jmatchq /f input.txt
If you only want the first occurrence in the file:

Code: Select all

call jrepl "StartTriggerString([\s\S]*?)EndTriggerString[\s\S]*" "$txt=$1" /m /jmatchq /f input.txt

Dave Benham

vin97
Posts: 27
Joined: 17 Apr 2020 08:30

Re: Using JREPL to extract only the first occurrence of a certain "bracket"?

#5 Post by vin97 » 20 Apr 2020 13:20

Thanks for all those tips!

Post Reply