Home > Mobile >  Inconsistent behaviour of my Regular Expression
Inconsistent behaviour of my Regular Expression

Time:10-13

I have a regular expression which is matching correctly when parameters are in their reversed order but not when they are in the intended order:

^\s*Proc\s [a-z_][0-9a-z_] \s*\({1}\s*([0-9a-z_ ,.] )\s* as (?:pin|bit|byte|word|dword|float|sbyte|sword|sdword|Long|slong|double|string\*[0-9] ,*)

matches this text just like I want to:

proc HMI_SendNumber(Value As Sword, Object As String*10)

But if I reverse the order of the parameters I am looking for...:

proc HMI_SendNumber(Object As String*10,Value As Sword)

...I only get a match on the first one, i.e. Object. It only occurs when String* is present, so I guess it has to do with the *10 element of it. Is there a way around this?

CodePudding user response:

No, you don't get "two matches", you only get one of:

Value As Sword, Object As String

See, how *10 is missing? That's because [0-9a-z_ ,.] does not allow * to match, too. Likewise your other text only has one match of:

Object As String

What do you really want? One match of all parameters? Multiple matches - one for each parameter? Because it's totally irrelevant to define all the as (1|2|3...) because it already matches your initial class. Your whole regex can be reduced to:

^\s*Proc\s [a-z_][0-9a-z_] \s*\(\s*([0-9a-z_ ,.] )\s*\)

if there would be no String*10 as data type. It can be fixed by including * as in:

^\s*Proc\s [a-z_][0-9a-z_] \s*\(\s*([0-9a-z_ ,.*] )\s*\)

Beware that this still is only one match, not multiple matches. The match itself may have your desired multiple parameters.

Also this has nothing to do with Delphi. It's slightly Visual Basic at best.

CodePudding user response:

I am now doing it in two stages, RegEx captures the string between the brackets and the I am splitting the resulting string into separate parameters.

  • Related