Particular question about a RegEx

Just starting out? Need help? Post your questions and find answers here.
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Particular question about a RegEx

Post 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?

Code: Select all

Debug “;” : Procedure Name()
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}"
Last edited by boddhi on Mon May 27, 2024 12:22 am, edited 3 times in total.
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
User avatar
STARGÅTE
Addict
Addict
Posts: 2233
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Particular question about a RegEx

Post 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.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Particular question about a RegEx

Post 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. :mrgreen:

Thanks.
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: Particular question about a RegEx

Post by AZJIO »

Code: Select all

(?mi)^\h*(?:Procedure[CDL$]{0,5}?(?:\h*\.[abcdfilqsuw])?\h+\K)[A-Za-z_]\w*\h*(?=\()
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Particular question about a RegEx

Post by boddhi »

For all intents and purposes, I've modified the RegEx to allow permitted spaces in certain locations.
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
User avatar
Piero
Addict
Addict
Posts: 929
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Particular question about a RegEx

Post by Piero »

:shock: WHAT?!

Code: Select all

~"\";\"":Procedure(:Debug #THIS_THREAD:)
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Particular question about a RegEx

Post by boddhi »

Piero wrote: :shock: WHAT?!

Code: Select all

~"\";\"":Procedure(:Debug #THIS_THREAD:)
:shock: Couldn't do less laconic and unexplicit!!!
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
User avatar
Piero
Addict
Addict
Posts: 929
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Particular question about a RegEx

Post by Piero »

boddhi wrote: Sun May 26, 2024 11:16 pm
Piero wrote: :shock: WHAT?!

Code: Select all

~"\";\"":Procedure(:Debug #THIS_THREAD:)
:shock: 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 ''"………
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Particular question about a RegEx

Post 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.
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Particular question about a RegEx

Post 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"
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: Particular question about a RegEx

Post 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*(?=\()
User avatar
Piero
Addict
Addict
Posts: 929
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Particular question about a RegEx

Post 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? :shock: :o :P :mrgreen:
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: Particular question about a RegEx

Post by AZJIO »

Code: Select all

{1}
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?

Code: Select all

(\w)* = \w*
without use "?" (non-greedy capture) will capture all the code up to the last parenthesis.

Code: Select all

.*\)
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

Code: Select all

Debug “;” : Procedure Name()
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.
Last edited by AZJIO on Mon May 27, 2024 12:57 am, edited 1 time in total.
User avatar
Piero
Addict
Addict
Posts: 929
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Particular question about a RegEx

Post 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:

Code: Select all

; Debug "p" : Procedure Name()
(quick test on regex101.com) :?
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: Particular question about a RegEx

Post 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

Code: Select all

(?:\v+|(?<!:):(?!:))[\h;]*
Post Reply