The parentheses are used to iterate over multiple results (the logic “if exist it then “yes” else “no”” is execute in a loop, repeating over each match; so if there is no match, the parentheses don’t execute.
Instead, try
if exists matches (regex "some_regex") of lines of files "C:\my_file" then "yes" else "no"
or, if you can deal with a boolean result instead, the following will give a True/False instead - exists matches (regex "some_regex") of lines of files "C:\my_file"
I agree with @JasonWalker 's answer, and it is what I would recommend. The following is more about relevance error suppression and some alternative options, but the extra complexity isn’t warranted.
There is some even weirder versions used to suppress errors.
Q: exists true whose (if true then THIS_IS_INVALID else false)
A: False
T: 0.104 ms
I: singular boolean
Q: (if it then "yes" else "no") of ( exists true whose (if true then ( exists matches (regex "some_regex") of lines of files "C:\my_file" ) else false) )
A: no
T: 0.174 ms
I: singular string
You’re right, my initial answer was solved, anyway for completeness i need to print the value of “it” if it exist, maybe concatenated with the origin file pathname, “No match” otherwise.
This piece of code is nested so i thoght to use it to not write something like that:
if (exists matches (regex "some_regex") of lines of files "C:\my_file") then (matches (regex "some_regex") of lines of files "C:\my_file") else "No match"
Another note on optimization. It is likely faster to only perform the regex on lines that may contain the useful data that the regex is going to extract rather than all lines of the file.
For instance, if you are extracting email addresses from the lines of the file, then it may be faster to do this:
matches (regex "some_regex") of lines containing "@" of files "C:\my_file"
I don’t know what the exact regex is that you are using, but if you can share it, it would be useful.
One problem with regex in general is that it is possible for it to take a longer time depending on the input provided to it and writing a regex to handle all possible cases well is hard and can often make it overly complicated. You can use relevance to only hand over a narrow scope of possible inputs to then be able to run a much more simplified regex against, and in many cases, you don’t need to use regex at all.