lua script and regex

You have encountered a problem using Far macros? Here you can get help.
Post Reply
farman
Posts: 118
Joined: Thu 03 Sep, 2009 11:32

lua script and regex

Post by farman » Tue 07 Oct, 2014 17:47

Hi,

i wanted to write a lua-macro doing some sort of a cleanup-filename-from-unwanted-chars in Editor, Dialog and Shell. But then I discovered that lua does only support patterns but no real regex. And now I need a little help.

How can I do this in LuaFar in a short way?
FileName="Long.File.With.Date.in.Name.2014.10.07.txt"
NewFileName="Long File With Date in Name 2014.10.07.txt"

1. Is there pure lua in far? Or is there a "regex support" (there are some libraries aoround in the web) added to LuaFar?

2. Is there a way to use the "Frontier Pattern" in LuaFar (http://lua-users.org/wiki/FrontierPattern)?

3. Can I add lookAhead/lookBehind/...-regex (ex. "(?=\d)") in a lua macro by using other functions from Far?

Thanx
:)
Last edited by HaRT on Tue 07 Oct, 2014 20:49, edited 1 time in total.
Reason: [c]

Shmuel
Posts: 5102
Joined: Thu 23 Mar, 2006 21:36
Location: Israel
Has thanked: 11 times
Been thanked: 140 times

Re: lua script and regex

Post by Shmuel » Tue 07 Oct, 2014 17:54

farman wrote:But then I discovered that lua does only support patterns but no real regex.
Open luafar_manual.chm and read the chapter "Regular expression functions".
farman wrote:How can I do this in LuaFar in a short way?
FileName="Long.File.With.Date.in.Name.2014.10.07.txt"
NewFileName="Long File With Date in Name 2014.10.07.txt"
NewFileName = FileName:gsub("%.", " ", 6)
farman wrote:1. Is there pure lua in far? Or is there a "regex support" (there are some libraries aoround in the web) added to LuaFar?

2. Is there a way to use the "Frontier Pattern" in LuaFar (http://lua-users.org/wiki/FrontierPattern)?

3. Can I add lookAhead/lookBehind/...-regex (ex. "(?=\d)") in a lua macro by using other functions from Far?
1. Yes, answered above.
2. Yes.
3. Yes, with regex library.

farman
Posts: 118
Joined: Thu 03 Sep, 2009 11:32

Re: lua script and regex

Post by farman » Tue 07 Oct, 2014 18:45

Thanx, shmuel. But it isn't so easy...

1. "CleanFilenName" means that the filenames changes. So [repl] doesn't work :(
FileName1="Long.File.With.Date.in.Name.2014.10.07.txt"
FileName2="Another.one.07.10.2014.example.name is here.txt"
....

I've tried my best searching through the manuals and example macros the last days and tried several things. But I have to say, that I'm still confused what to do. But now I'm very sure I made some errors. Can you please take a look the peaces and correct them?

Example 1: replace every instance of "\d." followed by \d (lookAhead) with...
_s2 = _s2:gsub ("(%d)[%.,](?=%d)", "%1-")

Example 2: replace with "Frontier Pattern": "." -> "*"
_s2 = _s2:gsub ("%f[%d][%.,]%f[%d]", "*")

And still the master question:
How do I replace the points (except between numbers) in the above examples?
Last edited by HaRT on Tue 07 Oct, 2014 20:50, edited 1 time in total.
Reason: [c]

Shmuel
Posts: 5102
Joined: Thu 23 Mar, 2006 21:36
Location: Israel
Has thanked: 11 times
Been thanked: 140 times

Re: lua script and regex

Post by Shmuel » Tue 07 Oct, 2014 18:54

farman
My reply was meant to show that you have real regular expressions available for Far macros, in addition to Lua patterns.
Unfortunately I don't always have time to solve particular regex needs of other people.
But this is not a Far-related question. You should just spend more time to construct a suitable regex.

farman
Posts: 118
Joined: Thu 03 Sep, 2009 11:32

Re: lua script and regex

Post by farman » Tue 07 Oct, 2014 19:50

To my knowledge this doesn't seem to be a regex problem. But maybe I missed something very important.

In Plugin RESearch I can find/replace all the "." between \d with this regex:
(?<=\d)[\.](?=\d)

In luafar (with gsub) this doesn't work.
(?<=%d)[%.](?=%d)

So, before i'm trying to fix my regex knoledge going through the web, can someone please give me a little hint?
Last edited by HaRT on Tue 07 Oct, 2014 20:50, edited 1 time in total.
Reason: [c]

Shmuel
Posts: 5102
Joined: Thu 23 Mar, 2006 21:36
Location: Israel
Has thanked: 11 times
Been thanked: 140 times

Re: lua script and regex

Post by Shmuel » Tue 07 Oct, 2014 20:05

farman wrote:In Plugin RESearch I can find/replace all the "." between \d with this regex:
(?<=\d)[\.](?=\d)
"(?<=\\d)[\\.](?=\\d)"
And this is not a Lua pattern, this is a pattern of regex library described in luafar_manual.chm.

farman
Posts: 118
Joined: Thu 03 Sep, 2009 11:32

Re: lua script and regex

Post by farman » Tue 07 Oct, 2014 20:27

Ah, double slashes!
Thanx again Shmuel. You've given me back my belief that far handles everything.

So, here's a complete version if someone is looking for something similar:
_s2 = regex.gsub(_s2, "(?<=\\d)[\\.](?=\\d)", "x")
Last edited by HaRT on Tue 07 Oct, 2014 20:51, edited 1 time in total.
Reason: [c]

User avatar
HaRT
Moderator
Posts: 8028
Joined: Tue 30 Aug, 2005 17:21
Location: Санкт-Петербург
Has thanked: 67 times
Been thanked: 100 times

Post by HaRT » Tue 07 Oct, 2014 20:52

farman
Please use the [c] and [code] tags when appropriate. For now, I've fixed your messages in this thread. No reply required.
Фар есть инструмент, а не нянька. © 2009 DrKnS

User avatar
John Doe
Бюрократ
Posts: 11100
Joined: Wed 27 Apr, 2005 20:42
Has thanked: 12 times
Been thanked: 68 times

Re: lua script and regex

Post by John Doe » Wed 08 Oct, 2014 06:57

farman wrote:Example 2: replace with "Frontier Pattern": "." -> "*"
_s2 = _s2:gsub ("%f[%d][%.,]%f[%d]", "*")
Try _s2 = string.gsub (_s2,"%f[%d][%.,]%f[%d]", "*") instead.

Explanation:
gsub called with colon syntax uses Selene Unicode library (see LuaFAR manual, "Unicode support").
Selene Unicode library does not support frontier pattern.
(There is related ticket at bugtracker, closed with resolution "won't fix".)

farman
Posts: 118
Joined: Thu 03 Sep, 2009 11:32

Re: lua script and regex

Post by farman » Wed 08 Oct, 2014 18:00

Thanx for your clarification, John Doe.
But

Code: Select all

_s2 = string.gsub (_s2,"%f[%d][%.,]%f[%d]", "*")
doesn't work for me. No problems with

Code: Select all

_s2 = regex.gsub(_s2, "(?<=\\d)[\\.](?=\\d)", "*")
And I was so happy to see the missing link... :(

Shmuel
Posts: 5102
Joined: Thu 23 Mar, 2006 21:36
Location: Israel
Has thanked: 11 times
Been thanked: 140 times

Re: lua script and regex

Post by Shmuel » Wed 08 Oct, 2014 18:59

_s2 = string.gsub(_s2, "(%d)[.,]%f[%d]", "%1*")

farman
Posts: 118
Joined: Thu 03 Sep, 2009 11:32

Re: lua script and regex

Post by farman » Thu 09 Oct, 2014 14:58

This usage is hard to remember (but now well documented here :))
I'll better use the regex in this cases.

AleXH
Posts: 1000
Joined: Sun 12 Apr, 2009 05:21
Been thanked: 1 time

lua script and regex

Post by AleXH » Thu 23 Oct, 2014 04:00

farman, RESearch - rename (selected) files

farman
Posts: 118
Joined: Thu 03 Sep, 2009 11:32

lua script and regex

Post by farman » Thu 23 Oct, 2014 05:51

Thanx, AleXH. I knew about REsearch (and the powerful batch mode). But I need lua and the flexibility of a programming language in this case.

Post Reply

Return to “Support and Troubleshooting”