DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: regex default value if nothing entered  (Read 5,830 times)

jinxx

  • Supporting Member
  • Joined in 2014
  • **
  • Posts: 10
regex default value if nothing entered
« on: September 19, 2020, 12:49 AM »
for example if I have this farr alias with respective value (some stuff redacted and formatted for readability):

slicevideo "(.*)" (.*) (.*) (.*)

shellexec /k "source file"=$$1 - cut time=$$2 - duration=$$3 - output name=$$4 |
ffmpeg.exe -i "$$1" -ss $$2 -c copy -t $$3 -vf "\ffmpeg output\$$4.mp4"

Say I wanted to sometimes omit the last value "$$4", and if I omitted it then "$$4" would go to the default value of "untitled" so i'd end up spitting out a file "untitled.mp4"

Is this possible? also for multiple trailing values

Obviously as-is it only works for the trailing $$'s

Nod5

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,175
    • Coding Snacks
    • Participated in NANY Challenge Three Times
    • Plugin Coding Medal
Re: regex default value if nothing entered
« Reply #1 on: September 19, 2020, 04:23 PM »
I don't think the FARR alias syntax has any feature for that kind of conditionality.

Not sure if there is a way to do it in the windows cmd part (left side of the | pipe).

My own solution in these kinds of cases is to write a small AutoHotkey script and have FARR run it with the parameters and do the conditional steps in the script. If you go for this option then consider doing only
Code
slicevideo (.*)
and separate out parameters from that one long string once in the script.

edit: Something like this

FARR alias
alias name: slicevideo
alias regex: slicevideo (.*)
alias result: C:\folder\FARR_slicevideo_helper.exe "$$1"

AutoHotkey script
Saved as FARR_slicevideo_helper.ahk and then compiled to FARR_slicevideo_helper.exe

Code: ( autohotkey)
#NoEnv
SetWorkingDir %A_ScriptDir%
#SingleInstance force

; FARR alias slicevideo helper script
; 2020-09-19

; FARR alias syntax:
; slicevideo "(.*)"
; where the regex sub pattern contains one of these string formats:
; - "<filepath>" <start-time-in-seconds> <duration-time-in-seconds> <out-file-name-no-ext>
; - "<filepath>" <start-time-in-seconds> <duration-time-in-seconds>
; - "<filepath>" <start-time-in-seconds>
; rule: only <filepath> can include spaces

; process command line input
; split at quote marks to array
; -> array item 1 = blank
; -> array item 2 = input file path
; -> array item 3 = all remaining params, space separated
ArrayA := StrSplit(A_Args[1], """")
; split above array item 3 at spaces to new array
ArrayB := StrSplit(ArrayA[3], " ")

File      := ArrayA[2]
StartTime := ArrayB[2]
Duration  := ArrayB[3]
OutFile   := ArrayB[4]

; input file must exist and starttime must be set
if !FileExist(File) or !StartTime
  ExitApp

; fallback output filename
if !OutFile
  OutFile := "untitled"

; fallback duration in seconds
if !Duration
  Duration := 60

; run cmd window that stays open and starts ffmpeg cut
Run %comspec% /k " ffmpeg.exe -i "%File%" -ss %StartTime% -c copy -t %Duration% -vf "\ffmpeg output\%OutFile%.mp4" "
« Last Edit: September 20, 2020, 08:19 AM by Nod5 »

wjamoe

  • Supporting Member
  • Joined in 2010
  • **
  • Posts: 99
    • Participated in NANY Challenge Once
    • Plugin Coding Medal
Re: regex default value if nothing entered
« Reply #2 on: September 25, 2020, 06:25 AM »
or you can make all 4 fields mandatory like:

slicevideo\s+"([^"]+)"\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)

\s+      = at least one space or tab (white space)
[^"]+  = at least one character until a quote
[^\s]+ = at least one character not being a white space
()        = remember contents for use in $$1 $$2 $$3 $$4

Maybe you want  to limit the characters for $$2 $$3 $$4 to make sure valid time format and filename

slicevideo\s+"([^"]+)"\s+([\d:]+)\s+([\d:]\w+)\s+(\w+)$

\w+    = at lease one word character 0-9 a-z A-Z and the underscore (word characters)
[\d:]+  =0-9 and ':' (in random order!)
[0-9:]  =0-9 and ':' (in random order)

if you want to extend the word characterset with '+' and '-'
replace
  \w+
by
  [\w+\-]+
please note you need a backslash just before the minus sign

-----
or a simple alternative
add a prefix (e.g. vid_) to each $$4 in the alias this will keep working when $$4 is empty like so:

shellexec /k "source file"=$$1 - cut time=$$2 - duration=$$3 - output name=vid_$$4 |
  ffmpeg.exe -i "$$1" -ss $$2 -c copy -t $$3 -vf "\ffmpeg output\vid_$$4.mp4"
« Last Edit: October 16, 2020, 05:31 PM by wjamoe »

Nod5

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,175
    • Coding Snacks
    • Participated in NANY Challenge Three Times
    • Plugin Coding Medal
Re: regex default value if nothing entered
« Reply #3 on: October 12, 2020, 08:24 AM »
Discussion in another thread https://www.donationcoder.com/forum/index.php?topic=50563.0 got me thinking...

An alternative way to do what OP here wants withouth using an external AutoHotkey script is to set up multiple aliases, one for each possible use case.

case 1 would be your original example where all four parameters are typed. Let us call that alias 1. We could there use the regex wjamoe suggested which requires all four parameters to be typed.
regex: slicevideo\s+"([^"]+)"\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)
results:
Code
shellexec /k "source file"=$$1 - cut time=$$2 - duration=$$3 - output name=$$4 | ffmpeg.exe -i "$$1" -ss $$2 -c copy -t $$3 -vf "\ffmpeg output\$$4.mp4"

case2 
Quote
Say I wanted to sometimes omit the last value "$$4", and if I omitted it then "$$4" would go to the default value of "untitled" so i'd end up spitting out a file "untitled.mp4"
covered by alias 2:
regex: slicevideo\s+"([^"]+)"\s+([^\s]+)\s+([^\s]+)
results:
Code
shellexec /k "source file"=$$1 - cut time=$$2 - duration=$$3 - output name=untitled | ffmpeg.exe -i "$$1" -ss $$2 -c copy -t $$3 -vf "\ffmpeg output\untitled.mp4"

You can create additional sibling aliases for the other cases you want to cover.
The drawback with this approach is that it is unwieldy to overview and if you want to later on e.g. change the ffmpeg path you need to edit multiple aliases.

Original address: https://www.donationcoder.com/forum/index.php?topic=50478.0