Sign in to follow this  
Followers 0
MarcusFernstrom

Regex lookbehind help

14 posts in this topic

So I'm trying to make a regex that matches yuidoc.

Here's an example:

/**
* @method myFunction
*/
function myFunction( param1, param2 ){
var x = arguments.param1;
var y = arguments.param2;
return x+y;
}

function secondFunction( param1, param2 ){
var x = arguments.param1;
var y = arguments.param2;
return x+y;
}

I'm trying to match any function that is *not* Yuidoc'ed, so basically I'm trying to do a lookbehind, looking for functions that are not preceeded by */

I have a pattern that matches the first example above, but can't figure out how to turn it into a negative lookbehind, so it only matches the second one.

/**(?:rn*)*rn*/rn(.*)

I've tried (?<!/**(?:rn*)*rn*/)rn(.*) and other variations but no success.

Share this post


Link to post
Share on other sites



What should your regex return with what the string you have posted?


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

I'm relatively used to regex, but not the AutoIt variety, and I've never used negative lookbehind before so that's my main issue.

But the idea is to grab the whole function block, do some processing and replace the original block with the processed one.

Share this post


Link to post
Share on other sites

Your look behind looks right, but to be honest, without seeing the EXACT output you'd expect in each array element, it's hard for me (this late to process).

Being javascript, you may need to take into account DotAll, (?s)

Personally, I'd do it in parts.

The first part being clearing all the yuidoc'ed items, something like:

Global $gsz_Str = "javascript string"; some string here

; strip all yuidoc'ed items
$gsz_Str = StringRegExpReplace($gsz_Str, _
    "(?s)((?:^|\v)\s*/\*\*.+?\*/)", "")

The second, would probably make returning your code blocks fairly simple.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

I'm reading a file, and need to replace the non-yuidoc functions with yuidoc ones, then save it out.

So my idea of the process is to:

Get a list of all the matches

Process the list, making the changes

Do a replace using the changes and the list of matches

If the regex looks ok, I must be missing something else.

Share this post


Link to post
Share on other sites

Ahh, I think I may get it now, you want to create the yuidoc's for your functions/classes etc like we have for our headers in our include files?


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

I didn't know the AutoIt headers use it, but yes, the goal is to automatically yuidoc functions that aren't already.

Another way that could work for a beta version is to strip any existing yuidoc (Easy, straight forward match/replace) and then yuidoc everything.

Share this post


Link to post
Share on other sites

I provided the code for the strip I believe.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Yeah I took it for a run with just matches, seemed to grab em

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Here's an example of using look behind. The regexp looks behind for yuidoc, followed by the pattern we want to match.

;

Local $sRegExp = '(?<=(\*/))(\s*pattern)'

MsgBox(0, '', StringRegExp('   pattern', $sRegExp)) ; Returns 0
MsgBox(0, '', StringRegExp('*/   pattern', $sRegExp)) ; Returns 1

;

Edit

Sorry I missread the first post. I tried the negative look behind, but didn't get it to work in the small amount of time I had available to test it.

Edited by czardas

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I think you want to have something like this : http://regex101.com/r/qG4pZ6/1

But I don't understand why it doesn't work with AutoIt... :blink:

 

Sometimes it can be hard to tell whether there's a bug in your own code or in the engine. Sometimes it's in both, which makes you feel slightly less stupid knowing it's not only your fault.

Edited by czardas

Share this post


Link to post
Share on other sites

Often the easiest way is to get around  :)

First blow off the yuidoc'ed part(s) then split what remains

#Include <Array.au3>

$txt = FileRead("1.txt")
$res = StringRegExpReplace($txt, '(?s)(/\*\*.*?function.*?)(?=function)', "")
;Msgbox(0,"", $res)

$res = StringRegExp($res, '(?s)(function.*?)\s*(?=function|\z)', 3)
 _ArrayDisplay($res)
; Msgbox(0,"", $res[0])

This allows conditionals {} inside funcs etc

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

This example captures the functions that do not have "*/" and linefeed preceding. Then extracts the function's name and adds a new header to the user function.

#include <Array.au3>

$txt = FileRead("Test2.txt")
$res = """" & StringRegExpReplace($txt, '(?sm)(?<!\*/\v\v)(^function.+?\})', """ & _Yuidoc(""\1"") & """) & """";
;ConsoleWrite( $res & @LF)
;ConsoleWrite("------------------------------------" & @LF)
ConsoleWrite(Execute($res) & @LF)


Func _Yuidoc($sFunc)
    Local $Ret = "/**" & @CRLF
    Local $sFuncName = "* @method " & StringRegExpReplace($sFunc, "(?s)function\h([^\(]+).+", "\1") & @CRLF
    Return $Ret & $sFuncName & "*/" & @CRLF & $sFunc
EndFunc   ;==>_Yuidoc

Edit: RE pattern was '(?sm)(?<!*/v)(^function.+?})'. Changed to
'(?sm)(?<!*/vv)(^function.+?})' to allow for @CRLF (2 vertical white spaces @CR and @LF) after "*/".

Edited by Malkey

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0