brother.gabriel | 4 May 2011 18:46
Gravatar

regex match

Hello! (Sheri?)
I am trying to regex something.  I want to find this single phrase in an entire file, so I readall the file and
use this here regex to find it:

local whatversionpat = regex.pcrecompile(?~(?im-sx)(?<=const\s+versionnumber\$\s+=\s+")\d+\.\d+(?=")~)

-------Here is part of the file:

$source
$GENFREE
$ACCELERATOR CalcKeys
Raw CalcKeys AS HACCEL
GUI "TreasureCalc", pixels, icon, 100

const blatdll$ = appexepath$ & "blat.dll"
Declare Function BlatSend Lib "blat.dll" Alias "Send" (ByVal sCmd As String) As Integer

const VersionNumber$ = "2.5"		'Monday, May 1, 2011

'pieces of strings to assemble for email

-----I am trying to find this:
const VersionNumber$ = "2.5"
----and grab into a variable, only this
2.5

My regex is close, I think, but needs something.
I appreciate any help!

------------------------------------
(Continue reading)

Sheri | 5 May 2011 00:04
Picon

Re: regex match

On 5/4/2011 12:46 PM, brother.gabriel wrote:
> Hello! (Sheri?)
> I am trying to regex something.  I want to find this single phrase in an entire file, so I readall the file and
use this here regex to find it:
>
> local whatversionpat = regex.pcrecompile(?~(?im-sx)(?<=const\s+versionnumber\$\s+=\s+")\d+\.\d+(?=")~)

Hi Brother Gabriel-Marie,

When you run that command, you should be seeing an error dialog, I do:

ERRORS: regex.pcreCcompile: PCRE compilation failed at offset 43: 
lookbehind assertion is not fixed length

You cannot use a plus quantifier in a lookbehind assertion. If the 
number of spaces varies, you could keep the quantifiers, get rid of the 
lookbehind, and instead use \K to signify where the match start should 
be set.

I guess they don't don't hurt anything, but since your pattern doesn't 
include caret, dollar or dot metacharacters, I see no need for multiline 
or (negated) dotall options. There's also no arbitrary whitespace in the 
pattern, so no need of the extended option.

And of course compiling a pattern returns the handle to a compiled 
pattern, not a match.

To get the match you could use whatversionpat.pcreMatch and include in 
the parameters the name of a variable (in quotes). Afterwards the 
variable with that name will have the version number. The default return 
(Continue reading)

brother.gabriel | 5 May 2011 02:17
Gravatar

Re: regex match

Sheri, thank you very much!  Can you show me where the \K should go?  I don't quite understand.  There are . and $
in the pattern, but they are escaped - is that a problem?  Also, just what is arbitrary white space?  

To get the match into a variable, would this be right?
local whatversion = whatversionpat.pcreMatch(whatfile)

After I find that version number, I will need to replace it with an updated version number.  Basically, I want
to look for the version number in my code file, which is decided in the const definition, catch it into a
variable, and then update all the readme files with the new version number.  I keep forgetting to put it in
all the different places.

--- In powerpro-beginners@..., Sheri <silvermoonwoman <at> ...> wrote:
>
> On 5/4/2011 12:46 PM, brother.gabriel wrote:
> > Hello! (Sheri?)
> > I am trying to regex something.  I want to find this single phrase in an entire file, so I readall the file and
use this here regex to find it:
> >
> > local whatversionpat = regex.pcrecompile(?~(?im-sx)(?<=const\s+versionnumber\$\s+=\s+")\d+\.\d+(?=")~)
> 
> Hi Brother Gabriel-Marie,
> 
> When you run that command, you should be seeing an error dialog, I do:
> 
> ERRORS: regex.pcreCcompile: PCRE compilation failed at offset 43: 
> lookbehind assertion is not fixed length
> 
> You cannot use a plus quantifier in a lookbehind assertion. If the 
> number of spaces varies, you could keep the quantifiers, get rid of the 
> lookbehind, and instead use \K to signify where the match start should 
(Continue reading)

Sheri | 5 May 2011 07:51
Picon

Re: regex match

--- In powerpro-beginners@..., "brother.gabriel"
<brgabriel <at> ...> wrote:
>
> Sheri, thank you very much! Can you show me where the \K
> should go? I don't quite understand. There are . and $ in
> the pattern, but they are escaped - is that a problem? Also,
> just what is arbitrary white space?

The \K would go where your present lookbehind ends.

dot and dollar are metacharacters only when they are not escaped.

you included an "x" (extended) option. don't know why you did it, but see the regex.chm options page for an
explanation of when to use it (i.e., to ignore white space in a pattern - but you had none).

  
> 
> To get the match into a variable, would this be right?
> local whatversion = whatversionpat.pcreMatch(whatfile)

no, as I said before pcreMatch returns a position. Try:

local whatversion
local position = hpat.pcreMatch(filecontent,"","whatversion")

> 
> After I find that version number, I will need to replace it
> with an updated version number. Basically, I want to look
> for the version number in my code file, which is decided in
> the const definition, catch it into a variable, and then
(Continue reading)

brother.gabriel | 5 May 2011 16:57
Gravatar

Re: regex match

Sheri, like this?:

local whatversionpat = regex.pcrecompile(?~(?is)(?<=const\K)(?<=versionnumber\$=\K")\d+\.\d+(?=")~)

I tried this too, to no avail:
local whatversionpat = regex.pcrecompile(?~(?is)(?=const\s+versionnumber\$\s+=\s+"\K)\d+\.\d+(?=")~)

It doesn't throw an error any more, it just returns nothing.

--- In powerpro-beginners@..., "Sheri"
<silvermoonwoman <at> ...> wrote:
>
> --- In powerpro-beginners@..., "brother.gabriel"
<brgabriel <at> > wrote:
> >
> > Sheri, thank you very much! Can you show me where the \K
> > should go? I don't quite understand. There are . and $ in
> > the pattern, but they are escaped - is that a problem? Also,
> > just what is arbitrary white space?
> 
> The \K would go where your present lookbehind ends.
> 
> dot and dollar are metacharacters only when they are not escaped.
> 
> you included an "x" (extended) option. don't know why you did it, but see the regex.chm options page for an
explanation of when to use it (i.e., to ignore white space in a pattern - but you had none).
> 
>   
> > 
> > To get the match into a variable, would this be right?
(Continue reading)

Sheri | 5 May 2011 18:30
Picon

Re: regex match

--- In powerpro-beginners@..., "brother.gabriel"
<brgabriel <at> ...> wrote:
>
> Sheri, like this?:
> 
> local whatversionpat = regex.pcrecompile(?~(?is)(?<=const\K)(?<=versionnumber\$=\K")\d+\.\d+(?=")~)
> 
> I tried this too, to no avail:
> local whatversionpat = regex.pcrecompile(?~(?is)(?=const\s+versionnumber\$\s+=\s+"\K)\d+\.\d+(?=")~)

Hi again,

I asked you to get rid of the lookbehind assertion and use \K instead, that the \K would go at the end of the part
previously included in the lookbehind.

If this had been the lookbehind:
(?<=abc)

this would have been how to convert for use with \K :
abc\K

There should be only one \K in the pattern. \K means "this is where the desired match starts"

You want the match to start at the beginning of the version number.

The benefit of using \K is that the portion of the pattern that precedes it does not have to be fixed length.
Therefore the plus quantifier is allowed there. As I recall your lookbehind had a couple of \s+ in it. Since
a lookbehind must have a fixed length \s+ is not permitted inside a lookbehind. I hope you saw the related
error message. If you are suppressing error messages, you are losing valuable info.

(Continue reading)

brother.gabriel | 5 May 2011 20:07
Gravatar

Re: regex match

Thank you for your patience, Sheri.

I finally got it with:
local whatversionpat = regex.pcrecompile(?~(?is)const\s+versionnumber\$\s+=\s+"\K\d+\.\d+(?=")~)

I guess I thought it was a lookbehind only if it had a < in the grouping.  I wasn't sure whether the \K went in
place of the lookbehind statement or in place of it (I am hard headed)- Sorry for the hassle!  The other
problem I had was I was running the statement through the PP Regular Expression Tester and it finding "Num"
- but PP finds the version number just like we want.  Strange, no?  

And to explain myself a bit, I was using a program called Expresso to build the regex statement (visual
interface), and it doesn't work quite the same way as the pcre.  I wrote to them about the specification and
am waiting to hear back.

Okay, well, thank you for the lessons!

--- In powerpro-beginners@..., "Sheri"
<silvermoonwoman <at> ...> wrote:
>
> --- In powerpro-beginners@..., "brother.gabriel"
<brgabriel <at> > wrote:
> >
> > Sheri, like this?:
> > 
> > local whatversionpat = regex.pcrecompile(?~(?is)(?<=const\K)(?<=versionnumber\$=\K")\d+\.\d+(?=")~)
> > 
> > I tried this too, to no avail:
> > local whatversionpat = regex.pcrecompile(?~(?is)(?=const\s+versionnumber\$\s+=\s+"\K)\d+\.\d+(?=")~)
> 
> Hi again,
(Continue reading)

Sheri | 6 May 2011 00:15
Picon

Re: regex match

--- In powerpro-beginners@..., "brother.gabriel"
<brgabriel <at> ...> wrote:
>
> And to explain myself a bit, I was using a program called
> Expresso to build the regex statement (visual interface),
> and it doesn't work quite the same way as the pcre. I wrote
> to them about the specification and am waiting to hear back.

I would not recommend using different flavors of regex for composing your regular expressions. I believe
Expresso is for dot net, and dot net's regex doesn't even have \K

There is a good regex tester that was designed for use with Powerpro and the regex plugin. It is
regexDialog.powerpro and is included in the complete distribution file for the regex plugin:
<http://tech.groups.yahoo.com/group/power-pro/files/Plug-ins_and_add-ons/regex210Release.zip>

FYI, you can increase the maximum amount of text that can be written in the subject and result areas of the
tester at line 73 of the regexDialog script.

You won't need anything from the zip but the regexdialog script file and its resources. The regex.dll file
and other files are included with Powerpro, and various files in the zip are currently the same as or older
than those distributed with Powerpro.

The regexdialog script itself requires the dialog and dll plugin.

Regards,
Sheri

------------------------------------

PowerPro can be found here: http://www.ppro.org/
(Continue reading)

brother.gabriel | 6 May 2011 18:17
Gravatar

Re: regex match

Thanks, Sheri, that regex tester is the one I was referring to.  I have been using the tester.  However,
expresso is a builder, which is very nice.  They pointed me to a reference chart that compares "flavours" of
regex.  I know it only does .net regex, however, and it certainly doesn't do \K.  Too bad PP doesn't have a
regex builder!

--- In powerpro-beginners@..., "Sheri"
<silvermoonwoman <at> ...> wrote:
>
> --- In powerpro-beginners@..., "brother.gabriel"
<brgabriel <at> > wrote:
> >
> > And to explain myself a bit, I was using a program called
> > Expresso to build the regex statement (visual interface),
> > and it doesn't work quite the same way as the pcre. I wrote
> > to them about the specification and am waiting to hear back.
> 
> I would not recommend using different flavors of regex for composing your regular expressions. I believe
Expresso is for dot net, and dot net's regex doesn't even have \K
> 
> There is a good regex tester that was designed for use with Powerpro and the regex plugin. It is
regexDialog.powerpro and is included in the complete distribution file for the regex plugin:
> <http://tech.groups.yahoo.com/group/power-pro/files/Plug-ins_and_add-ons/regex210Release.zip>
> 
> FYI, you can increase the maximum amount of text that can be written in the subject and result areas of the
tester at line 73 of the regexDialog script.
> 
> You won't need anything from the zip but the regexdialog script file and its resources. The regex.dll file
and other files are included with Powerpro, and various files in the zip are currently the same as or older
than those distributed with Powerpro.
> 
(Continue reading)

Sheri | 6 May 2011 22:18
Picon

Re: regex match

--- In powerpro-beginners@..., "brother.gabriel"
<brgabriel <at> ...> wrote:
>
> Thanks, Sheri, that regex tester is the one I was referring to.  I have been using the tester.

You said "The other problem I had was I was running the statement through the PP Regular Expression Tester
and it finding "Num" - but PP finds the version number just like we want. Strange, no?"

I'm not able to reproduce that. When I use your test data in regexDialog, your first pattern to Match gives an
error (lookbehind must be fixed length). The 2nd and 3rd patterns give "no match". The last one highlights 2.5

Regards,
Sheri

------------------------------------

PowerPro can be found here: http://www.ppro.org/
and here: http://ppro.pcrei.com/Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/powerpro-beginners/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/powerpro-beginners/join
    (Yahoo! ID required)

<*> To change settings via email:
(Continue reading)


Gmane