[Solved] find replace between [] problem

Discuss the word processor

[Solved] find replace between [] problem

Postby vector » Wed Jun 13, 2018 4:04 am

Hi all,
I am trying to convert parts of a chordpro text file.
Basically it has music chords denoted within [], inserted into the lyrics text.
It also has special lines, denoted with {} which I would like to manipulate..
I am aware of the chordtransposer extension but its not quite doing everything id hope.

But first the [] problem
Find attached a chordpro file for reference. Simnply open with openoffice(its just a basic text file)
In some cases I want to remove the [chords] completely.
So I need an expression to find all characters within []
Code: Select all   Expand viewCollapse view
[G]City girls just [Gmaj7]seem

to create this line (by replacing with nothing)
Code: Select all   Expand viewCollapse view
City girls just seem

I used the F&R regular expression \[.\] which finds [G] but not [Gmaj7] because . only looks for one character
but \[.*\] does something strange as it selects all text between the first [ and the last ] on the line.
[G]City girls just [Gmaj7]seem.
instead of
[G]City girls just [Gmaj7]seem

I cant quite fathom the expressions and how they work even after reading the help.
\[.....\] will find the [Gmaj7] but not the simple [G]

Would appreciate some help as I have tried various different concoctions but alas im obviously playing in the dark
Attachments
Lyin Eyes - Emnem.txt
(2.88 KiB) Downloaded 66 times
Last edited by RoryOF on Wed Jun 13, 2018 5:04 pm, edited 2 times in total.
Reason: Added green tick [RoryOF, Moderator]
open Office 4 Windows/Ubuntu
vector
 
Posts: 7
Joined: Wed Jun 13, 2018 3:32 am

Re: find replace between [] problem

Postby FJCC » Wed Jun 13, 2018 5:32 am

Since [ is a special character in regular expressions, this looks messy. Try searching for
Code: Select all   Expand viewCollapse view
\[[^\]]*\]

That means
\[ = [
[^\]] = any character that is not ]
* = zero or more
\] = ]
AOO 3.4 or 4.1 on MS Windows XP ( before 2013-08-03) or Windows 7
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 6697
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: find replace between [] problem

Postby vector » Wed Jun 13, 2018 8:14 am

awesome that did it. Im sure I had something close to that at some point.
cheers
open Office 4 Windows/Ubuntu
vector
 
Posts: 7
Joined: Wed Jun 13, 2018 3:32 am

Re: [SOLVED] find replace between [] problem

Postby keme » Wed Jun 13, 2018 9:02 am

As you found, regex search defaults to "greedy matching" within paragraph (largest possible matching section). To specify "lazy" (non-greedy) matching, i.e. smallest possible matching section, add a question mark to the quantifier. Also, if you want to specify "at least one", use plus instead of asterisk. The solution provided by FJCC does the job, but lazy matching allows for cleaner expressions, so it's easier to avoid mistakes.
Code: Select all   Expand viewCollapse view
\[.+?\]

For chordpro files and other plaintext work, I find that Notepad++ is a better tool. Np++ regex search also supports matching across multiple paragraphs (check ". matches newline"), for which you need a plugin in Writer.
User avatar
keme
Volunteer
 
Posts: 2925
Joined: Wed Nov 28, 2007 10:27 am
Location: Egersund, Norway

Re: [Solved] find replace between [] problem

Postby vector » Fri Jun 15, 2018 2:44 am

ah yes even cleaner thankyou
open Office 4 Windows/Ubuntu
vector
 
Posts: 7
Joined: Wed Jun 13, 2018 3:32 am


Return to Writer

Who is online

Users browsing this forum: Google [Bot] and 35 guests