[Solved] find replace between [] problem

Discuss the word processor
Post Reply
vector
Posts: 7
Joined: Wed Jun 13, 2018 3:32 am

[Solved] find replace between [] problem

Post by vector »

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

[G]City girls just [Gmaj7]seem
to create this line (by replacing with nothing)

Code: Select all

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 159 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
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: find replace between [] problem

Post by FJCC »

Since [ is a special character in regular expressions, this looks messy. Try searching for

Code: Select all

\[[^\]]*\]
That means
\[ = [
[^\]] = any character that is not ]
* = zero or more
\] = ]
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
vector
Posts: 7
Joined: Wed Jun 13, 2018 3:32 am

Re: find replace between [] problem

Post by vector »

awesome that did it. Im sure I had something close to that at some point.
cheers
open Office 4 Windows/Ubuntu
User avatar
keme
Volunteer
Posts: 3699
Joined: Wed Nov 28, 2007 10:27 am
Location: Egersund, Norway

Re: [SOLVED] find replace between [] problem

Post by keme »

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

\[.+?\]
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.
vector
Posts: 7
Joined: Wed Jun 13, 2018 3:32 am

Re: [Solved] find replace between [] problem

Post by vector »

ah yes even cleaner thankyou
open Office 4 Windows/Ubuntu
Post Reply