Page 1 of 1
Using JREPL to extract only the first occurrence of a certain "bracket"?
Posted: 17 Apr 2020 08:42
by vin97
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:
Re: Using JREPL to extract only the first occurrence of a certain "bracket"?
Posted: 17 Apr 2020 15:09
by siberia-man
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
Re: Using JREPL to extract only the first occurrence of a certain "bracket"?
Posted: 17 Apr 2020 18:05
by vin97
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
Re: Using JREPL to extract only the first occurrence of a certain "bracket"?
Posted: 17 Apr 2020 18:12
by dbenham
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
Re: Using JREPL to extract only the first occurrence of a certain "bracket"?
Posted: 20 Apr 2020 13:20
by vin97
Thanks for all those tips!