Page 1 of 2
Particular question about a RegEx
Posted: Sun May 26, 2024 7:44 am
by boddhi
Hello,
I created the following RegEx to detect a procedure in a code (the arguments part was deliberately omitted):
Code: Select all
RegEx.s="(?i)^(\s*;*)procedure(dll|cdll|c)? *(\.(a|b|c|d|f|i|l|q|s|u|w))? *_*[a-z]{1}(\w)*\({1}.*\){1}"
It works very well for the following cases:
Code: Select all
Procedure Name() => Accepted
Procedure Name() => Accepted
; Procedure Name() => Comment => Rejected
But, because PB allows this kind of syntax, how do you ensure that a case like the following will be accepted?
Thanks for your help.
[EDITS] : Question not solved but below RegEx updated to authorize spaces in certain locations and prohibit ProcedureReturn :
Code: Select all
(?i)procedure(?!return)(dll|cdll|c)? *( *\. *(a|b|c|d|f|i|l|q|s|u|w))? *_*[a-z]{1}(\w)* *\({1}.*\){1}"
Re: Particular question about a RegEx
Posted: Sun May 26, 2024 8:15 am
by STARGÅTE
You need to check the full syntax.
While you are test for procedure, you have to check for comment and strings as well but just evaluate your case.
In such case, ";" is matched as string and not as command.
Re: Particular question about a RegEx
Posted: Sun May 26, 2024 8:30 am
by boddhi
Hi,
STARGÅTE wrote:
You need to check the full syntax.

that's what I expected.
I wanted to avoid this and try to evaluate all this, if possible (a little too naively, lazy as I am) with a single RegEx.
Thanks.
Re: Particular question about a RegEx
Posted: Sun May 26, 2024 8:38 am
by AZJIO
Code: Select all
(?mi)^\h*(?:Procedure[CDL$]{0,5}?(?:\h*\.[abcdfilqsuw])?\h+\K)[A-Za-z_]\w*\h*(?=\()
Re: Particular question about a RegEx
Posted: Sun May 26, 2024 8:44 am
by boddhi
For all intents and purposes, I've modified the RegEx to allow permitted spaces in certain locations.
Re: Particular question about a RegEx
Posted: Sun May 26, 2024 11:09 pm
by Piero
WHAT?!
Code: Select all
~"\";\"":Procedure(:Debug #THIS_THREAD:)
Re: Particular question about a RegEx
Posted: Sun May 26, 2024 11:16 pm
by boddhi
Piero wrote:
WHAT?!
Code: Select all
~"\";\"":Procedure(:Debug #THIS_THREAD:)

Couldn't do less laconic and unexplicit!!!
Re: Particular question about a RegEx
Posted: Sun May 26, 2024 11:38 pm
by Piero
boddhi wrote: Sun May 26, 2024 11:16 pm
Piero wrote:
WHAT?!
Code: Select all
~"\";\"":Procedure(:Debug #THIS_THREAD:)

Couldn't do less laconic and unexplicit!!!
Forgive me boddhi, I'm having problems with the forum atm… anyaway:
If you want to consider ":", IMHO you are seeking for problems and slowness if you want to solve it all with a single regexp (better check with stringfield?) also, I see “” instead of ''"………
Re: Particular question about a RegEx
Posted: Sun May 26, 2024 11:56 pm
by boddhi
Piero wrote:
If you want to consider ":", IMHO you are seeking for problems and slowness if you want to solve it all with a single regexp (better check with stringfield?)
Yes I confirm, for want of a better solution, that's what I had to do! Combine RegEx(es) and StringField() to solve my problem.
Re: Particular question about a RegEx
Posted: Mon May 27, 2024 12:11 am
by boddhi
boddhi wrote: Sun May 26, 2024 11:56 pm
Piero wrote:
If you want to consider ":", IMHO you are seeking for problems and slowness if you want to solve it all with a single regexp (better check with stringfield?)
Yes I confirm, for want of a better solution, that's what I had to do! Combine RegEx(es) and StringField() to solve my problem.
Note : RegEx updated to prohibit "ProcedureReturn"
Re: Particular question about a RegEx
Posted: Mon May 27, 2024 12:14 am
by AZJIO
Code: Select all
^(\s*;*)
(\v|:)[\h;])
(\v|(?<!:):(?!:))[\h;]*
[\h:;]+
Code: Select all
(\v+|(?<!:):(?!:))[\h;]*(?:Procedure[CDL$]{0,5}?(?:\h*\.[abcdfilqsuw])?\h+\K)[A-Za-z_]\w*\h*(?=\()
Re: Particular question about a RegEx
Posted: Mon May 27, 2024 12:24 am
by Piero
boddhi wrote: Mon May 27, 2024 12:11 am
Note : RegEx updated to prohibit "ProcedureReturn"
Did you see my post about "exclusive regexps"?
https://www.purebasic.fr/english/viewtopic.php?t=84113
What if the ":" is into a string?

Re: Particular question about a RegEx
Posted: Mon May 27, 2024 12:29 am
by AZJIO
there is no need to write this, it is by default
The "OR" group is used if at least one element is more than one character
Code: Select all
(a|b|c|d|f|i|l|q|s|u|w) = [abcdfilqsuw]
why put part of the name in a group?
without use "?" (non-greedy capture) will capture all the code up to the last parenthesis.
In general, I think capturing procedure parameters in parentheses is unproductive, since you may encounter nested functions and, accordingly, a bunch of parentheses. You will have to analyze the number of parentheses and whether the parentheses are inside quotes or inside comments, so it is unrealistic to make such a parser.
boddhi wrote: Sun May 26, 2024 7:44 am
This is the wrong way to encode. I don't think anyone will write a program in one line. Unless someone codes specifically to make your program unable to analyze code, I don’t see any other reason to write code that way.
Re: Particular question about a RegEx
Posted: Mon May 27, 2024 12:55 am
by Piero
AZJIO wrote: Mon May 27, 2024 12:14 am
Code: Select all
(\v+|(?<!:):(?!:))[\h;]*(?:Procedure[CDL$]{0,5}?(?:\h*\.[abcdfilqsuw])?\h+\K)[A-Za-z_]\w*\h*(?=\()
Seems to fail on:
(quick test on regex101.com)

Re: Particular question about a RegEx
Posted: Mon May 27, 2024 1:01 am
by AZJIO
Piero wrote: Mon May 27, 2024 12:55 am
Seems to fail on:
It doesn't crash -
RegExpPB. Because it uses the PCRE engine.
Added ?: so as not to add the group to the search