* Lua pattern match question
Lua pattern match question
I am trying a search and replace where the string is:
aa, aa - dddd-dd-dd bb - cc
into:
aa, aa - nnnn-nn-nn - cc; bb
a, c being any alpha character, b also, but may contain spaces, ' or - and d is a digit.
I have no problem doing it in regex, but can't get it done in Lua.
Help is much appreciated.
aa, aa - dddd-dd-dd bb - cc
into:
aa, aa - nnnn-nn-nn - cc; bb
a, c being any alpha character, b also, but may contain spaces, ' or - and d is a digit.
I have no problem doing it in regex, but can't get it done in Lua.
Help is much appreciated.
Re: Lua pattern match question
As always, right after asking the question I found an answer:
(.-)- (.-) (.-)%s%- (persoons%w+)
%1- %2 - %4; %3
Did what I wanted. I had been trying:
(.-)- (.-) (.-) - (persoons%w+)
but that didn't work. Changed it to the above and it did. However, don't know why.
BTW the 'persoons' part is just to select the right records.
(.-)- (.-) (.-)%s%- (persoons%w+)
%1- %2 - %4; %3
Did what I wanted. I had been trying:
(.-)- (.-) (.-) - (persoons%w+)
but that didn't work. Changed it to the above and it did. However, don't know why.
BTW the 'persoons' part is just to select the right records.
- tatewise
- Megastar
- Posts: 27088
- Joined: 25 May 2010 11:00
- Family Historian: V7
- Location: Torbay, Devon, UK
- Contact:
Re: Lua pattern match question
Presumably, you are talking about the Search and Replace plugin or are you writing your own plugin?
Anyway, I have moved this to the Plugin Discussions forum.
I don't see how either of your patterns come anywhere near your original specification.
BTW: To Select Records why not use the Search Scope options at the top?
Anyway to match aa, aa - dddd-dd-dd bb - cc
Would need %w%w, %w%w %- %d%d%d%d%-%d%d%-%d%d .. %- %w%w (without any captures to keep it simple)
One important thing to recognise is that 'magic' characters such as hyphen - must be escaped %- to match themself.
So in your examples where you have - on its own it is NOT matching a literal hyphen.
I suspect it is only the uniqueness of persoons that it works at all.
Or are you saying aa and bb and cc are not necessarily two characters?
Anyway, I have moved this to the Plugin Discussions forum.
I don't see how either of your patterns come anywhere near your original specification.
BTW: To Select Records why not use the Search Scope options at the top?
Anyway to match aa, aa - dddd-dd-dd bb - cc
Would need %w%w, %w%w %- %d%d%d%d%-%d%d%-%d%d .. %- %w%w (without any captures to keep it simple)
One important thing to recognise is that 'magic' characters such as hyphen - must be escaped %- to match themself.
So in your examples where you have - on its own it is NOT matching a literal hyphen.
I suspect it is only the uniqueness of persoons that it works at all.
Or are you saying aa and bb and cc are not necessarily two characters?
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
Re: Lua pattern match question
Yes Mike, I am talking about Search an Replace and am using it to synch changed names of windows files to links in the FH database.
The file names are now:
last name, first name - yyyy-mm-dd - tekst; place (with 'tekst' containing a first part that's systematically used)
In FH the references are:
last name, first name - yyyy-mm-dd place - tekst
I guess I went wrong with using - without the escape.

The file names are now:
last name, first name - yyyy-mm-dd - tekst; place (with 'tekst' containing a first part that's systematically used)
In FH the references are:
last name, first name - yyyy-mm-dd place - tekst
I guess I went wrong with using - without the escape.
It worked, but you are right in as far as that I don't understand why.I don't see how either of your patterns come anywhere near your original specification.
- tatewise
- Megastar
- Posts: 27088
- Joined: 25 May 2010 11:00
- Family Historian: V7
- Location: Torbay, Devon, UK
- Contact:
Re: Lua pattern match question
Can you backtrack a little and explain how the Windows filenames and the Media record links got out of synch.
Are you saying tekst always starts with persoons ?
Please also confirm that you are changing the Media record File Links and not the Media record Title.
There must also be a filetype include in the filepath at the end,
e.g. last name, first name - yyyy-mm-dd - tekst; place.jpeg
Are you saying tekst always starts with persoons ?
Please also confirm that you are changing the Media record File Links and not the Media record Title.
There must also be a filetype include in the filepath at the end,
e.g. last name, first name - yyyy-mm-dd - tekst; place.jpeg
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
Re: Lua pattern match question
I changed the filenames of the media files in Windows (en masse) from the old structure to the new one. Hence I had to do the same in FH with the links (Media record Titles are not touched).
So using the very versatile Search an Replace plugin to change (en masse) the links seemed the best approach.
In Windows the files are in folders according to their type (e.g. birth, death, marriage etc.). In the filename there is text that describes the content of the file (like obituary or birth certificate). In Dutch an example is 'persoonskaart' or 'persoonslijst'. But for other folders it may be something else and I then use that (part of a) word in a new Search and Replace.
The reason I did it this way is because I got this working and didn't know how to make it more general.
Re BTW: the solution I used took care of the file extension (.jpg) too.
So using the very versatile Search an Replace plugin to change (en masse) the links seemed the best approach.
In Windows the files are in folders according to their type (e.g. birth, death, marriage etc.). In the filename there is text that describes the content of the file (like obituary or birth certificate). In Dutch an example is 'persoonskaart' or 'persoonslijst'. But for other folders it may be something else and I then use that (part of a) word in a new Search and Replace.
The reason I did it this way is because I got this working and didn't know how to make it more general.
Re BTW: the solution I used took care of the file extension (.jpg) too.
- tatewise
- Megastar
- Posts: 27088
- Joined: 25 May 2010 11:00
- Family Historian: V7
- Location: Torbay, Devon, UK
- Contact:
Re: Lua pattern match question
If you had started with the Search and Replace plugin and changed the File Links there, then it would have automatically changed the Windows filenames to match.
Now the File Links are all broken, then Search and Replace will just change them and not change any Windows filenames.
To match:
yyyy-mm-dd place - tekst
use Search:
(%d%d%d%d%-%d%d%-%d%d) (.+) %- (%w+)%.
Replace:
%1 %3; %2.
The lastname, forename part is not changing so can be disregarded.
The yyyy-mm-dd set of digits & hyphens is captured by (%d%d%d%d%-%d%d%-%d%d) and is a strong anchor.
The place is captured by (.+) which matches any characters and has one space character before and after.
The tekst is captured by (%w+) which matches any alphanumeric characters and has - space before and dot after which also acts as a strong anchor.
BTW: If the number of spaces between the captures is possibly more than one then add + to each space:
(%d%d%d%d%-%d%d%-%d%d) +(.+) +%- +(%w+)%.
Now the File Links are all broken, then Search and Replace will just change them and not change any Windows filenames.
To match:
yyyy-mm-dd place - tekst
use Search:
(%d%d%d%d%-%d%d%-%d%d) (.+) %- (%w+)%.
Replace:
%1 %3; %2.
The lastname, forename part is not changing so can be disregarded.
The yyyy-mm-dd set of digits & hyphens is captured by (%d%d%d%d%-%d%d%-%d%d) and is a strong anchor.
The place is captured by (.+) which matches any characters and has one space character before and after.
The tekst is captured by (%w+) which matches any alphanumeric characters and has - space before and dot after which also acts as a strong anchor.
BTW: If the number of spaces between the captures is possibly more than one then add + to each space:
(%d%d%d%d%-%d%d%-%d%d) +(.+) +%- +(%w+)%.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
Re: Lua pattern match question
Thanks Mike!
I never realized that Search and Replace could also change File Links. Indeed a far better approach, which I'll use in the future.
For now I'll use the pattern you suggest and get things in sync again.
I never realized that Search and Replace could also change File Links. Indeed a far better approach, which I'll use in the future.
For now I'll use the pattern you suggest and get things in sync again.