CALL JREPL not returning "wanted" results.... help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Craig
Posts: 2
Joined: 11 Jan 2021 07:49

CALL JREPL not returning "wanted" results.... help

#1 Post by Craig » 11 Jan 2021 08:06

I need help.
Line1.txt contains 1 line below and is located in the same folder as my batch file.

Code: Select all

<a href="/LunaMultiplayer/LunaMultiplayer/tree/0.27.0" class="muted-link css-truncate" title="0.27.0">
My batch plzwork.bat is as follows:

Code: Select all

@ECHO OFF
ECHO. %~n0: PLEASE WORK THIS TIME.
SETLOCAL DisableDelayedExpansion
CALL JREPL "([0-9]{1-3})\.([0-9]{1-3})\.([0-9]{1-3})" "" /P Regex /F "Line1.txt" /O "Version.txt"
ENDLOCAL
ECHO.
ECHO. %~n0:      Press any key to quit:
PAUSE>NUL
EXIT
This is what I get for Version.txt below when the script is run:

Code: Select all

<a href="/LunaMultiplayer/LunaMultiplayer/tree/0.27.0" class="muted-link css-truncate" title="0.27.0">
GRRRRRRRRRRR!!!!!!!
Question is, why does the output file Version.txt still contain the entire line from Line1.txt instead
of the result I want which is for Version.txt to equal a single line containing only "0.27.0" ???????

Craig
Posts: 2
Joined: 11 Jan 2021 07:49

Re: CALL JREPL not returning "wanted" results.... help

#2 Post by Craig » 11 Jan 2021 14:01

Thanks, just posting this has somehow enabled me to figure it out... lol
My pattern was wrong and I needed to include the option /MATCH

CALL JREPL "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}" "" /MATCH /F Line1.txt /O "Version.txt"

feel free to add any comments, however I'm good now.... PHEW!!

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

Re: CALL JREPL not returning "wanted" results.... help

#3 Post by dbenham » 12 Jan 2021 09:21

Much better, though that should give you two lines of output since 0.27.0 appears twice in your source. If you want only one then you need to figure out rules for how to choose which one you want.

Also, your pattern might give the wrong result if the source includes something like the following: 1234.567.8901
Your code will yield 234.567.890 - I doubt that would be a correct result. Maybe not a problem, but you should always consider the possibility of outliers and plan accordingly.

Note that [0-9] can more easily be represented as \d

If you assume that you always want to extract from the title, then either of the following would work:

Code: Select all

call jrepl ".*title=\q(\d{1,3}\.\d{1,3}\.\d{1,3})\q.*" "$1" /xseq /f "line1.txt" /o "version.txt"

Code: Select all

call jrepl "title=\q(\d{1,3}\.\d{1,3}\.\d{1,3})\q" "$txt=$1" /xseq /jmatch /f "line1.txt" /o "version.txt"
There are other solutions, but those are the first two that popped into mind.

Or if you know your source only has one title, and you always want the entire title value, then:

Code: Select all

call jrepl ".*title=\q(.*?)\q.*" "$1" /xseq /f "line1.txt" /o "version.txt"

Code: Select all

call jrepl "title=\q(.*)\q" "$txt=$1" /xseq /jmatch /f "line1.txt" /o "version.txt"
I'm just guessing, but it looks like your line1.txt might be the first line of some larger file, and you did some preprocessing to extract the first line. If I'm correct, then you can work directly with the original file

Code: Select all

call jrepl ".*title=\q(.*?)\q.*" "$1" /xseq  /inc 1 /f "original.txt" /o "version.txt"

Code: Select all

call jrepl "title=\q(.*)\q" "$txt=$1" /xseq /jmatch /inc 1 /f "original.txt" /o "version.txt"
If you want to get the value in an environment variable instead of a file, then remove the /O "VERSION.TXT" and add /RTN VERSION instead. The result will be in the VERSION variable when finished.


Dave Benham

Post Reply