Jump to content

Regex lookbehind help


Recommended Posts

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.

Link to comment
Share on other sites

  • Moderators

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

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.

Link to comment
Share on other sites

  • Moderators

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.

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.

Link to comment
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.

Link to comment
Share on other sites

  • Moderators

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?

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
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

Link to comment
Share on other sites

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...