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
Been thanked: 2 times

lua script and regex

Post by farman »

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 farman on Thu 01 Jan, 1970 01:00, edited 0 times in total.
Reason: [c]
Shmuel
Posts: 6815
Joined: Thu 23 Mar, 2006 21:36
Location: Israel
Has thanked: 41 times
Been thanked: 526 times

Re: lua script and regex

Post by Shmuel »

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
Been thanked: 2 times

Re: lua script and regex

Post by farman »

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 farman on Thu 01 Jan, 1970 01:00, edited 0 times in total.
Reason: [c]
Shmuel
Posts: 6815
Joined: Thu 23 Mar, 2006 21:36
Location: Israel
Has thanked: 41 times
Been thanked: 526 times

Re: lua script and regex

Post by Shmuel »

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
Been thanked: 2 times

Re: lua script and regex

Post by farman »

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 farman on Thu 01 Jan, 1970 01:00, edited 0 times in total.
Reason: [c]
Shmuel
Posts: 6815
Joined: Thu 23 Mar, 2006 21:36
Location: Israel
Has thanked: 41 times
Been thanked: 526 times

Re: lua script and regex

Post by Shmuel »

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
Been thanked: 2 times

Re: lua script and regex

Post by farman »

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 farman on Thu 01 Jan, 1970 01:00, edited 0 times in total.
Reason: [c]
User avatar
HaRT
Moderator
Posts: 10806
Joined: Tue 30 Aug, 2005 17:21
Has thanked: 220 times
Been thanked: 357 times

Post by HaRT »

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: 13801
Joined: Wed 27 Apr, 2005 20:42
Has thanked: 72 times
Been thanked: 425 times
Contact:

Re: lua script and regex

Post by John Doe »

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
Been thanked: 2 times

Re: lua script and regex

Post by farman »

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: 6815
Joined: Thu 23 Mar, 2006 21:36
Location: Israel
Has thanked: 41 times
Been thanked: 526 times

Re: lua script and regex

Post by Shmuel »

_s2 = string.gsub(_s2, "(%d)[.,]%f[%d]", "%1*")
farman
Posts: 118
Joined: Thu 03 Sep, 2009 11:32
Been thanked: 2 times

Re: lua script and regex

Post by farman »

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 »

farman, RESearch - rename (selected) files
farman
Posts: 118
Joined: Thu 03 Sep, 2009 11:32
Been thanked: 2 times

lua script and regex

Post by farman »

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”