Jump to content

Regex copy 2nd word of the line to the End of another Line


 Share

Recommended Posts

Hi, I was only be able to do this in Notepad++ partially. But can't replicate in SciTE. What I need to do is copy the function name and paste it couple lines down at the end of of another line. Thanks

;Code works in Notepad++ partially but not in SciTE 

RegEx Find: ^\b(Func)\b\s(.*?)([\(])(.*)([\)])(.*?)$
Replace: $1 $2$3$4$5$6 ;===>$2$3$5

From:

Func MyFunc($str)               ;copy this function name MyFunc() then paste behind EndFunc below
    ConsoleWrite($str&@LF)
EndFunc

To:

Func MyFunc($str)
    ConsoleWrite($str&@LF)
EndFunc   ;===>MyFunc()             ;<--Result in the comment behind EndFunc with MyFunc()

 

Link to comment
Share on other sites

:)

$txt = "Func MyFunc($str)  ;copy this function name MyFunc() then paste behind EndFunc below" & @crlf & _ 
    "    ConsoleWrite($str&@LF)" & @crlf & _ 
    "EndFunc"
Msgbox(0,"before", $txt)


$res = StringRegExpReplace($txt, '(?ms)^Func\h+(\w+).*?\K(?<=^EndFunc)', "  ;===> $1()")
Msgbox(0,"after", $res)


Edit : some comments

\w+  can be used because a func name may contain only letters/digits/underscores
\K  means : "please don't touch the above" ^_^
(?<=^EndFunc)  lookbehind, it matches the place right after the next "EndFunc" preceded by a start of line (allowed by the (?m) multiline option)
(?s)  allows .*?  to match newlines

 

Edited by mikell
Link to comment
Share on other sites

doesnt Tidy do this?

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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...