I need help with a regex problem I don't understand. Luuk?

Bulk Rename Utility How-To's

I need help with a regex problem I don't understand. Luuk?

Postby Ronstang » Tue Feb 17, 2026 9:07 am

I cannot find an answer to this issue anywhere and here I have always received solid help solving my issues in renaming things. I am not using BRU to do this but since the principle is exactly the same I was wondering if anyone could help me solve this issue?

I'm working with subtitles here where entire lines have an apostrophe at the beginning and at the end of the line and I want to change all text bounded by these apostrophes into italics. I have the replace expression that works, I just cannot get the find expression correct.

It needs to identify the first apostrophe as having no text characters before it and the last apostrophe as one with no text characters after it but then I don't know how to handle something like (parents') as a word in the sentence. Plus it needs to ignore apostrophes in contractions because they are not the beginning or end of a line of text.

Some I have tried don't see past the line breaks either and I do not have enough understanding to figure that out at all.

Examples:

the famous homes<br />of New York's Fifth Avenue.
Needs to become

<i>the famous homes<br />of New York's Fifth Avenue.</i>
I have tried the following find expressions:

([^']+)
but it stops at the apostrophe in "York's" and then moves on to the next line

^'(.*?)'$
This one works when there is only one line but if there is a line break it skips that whole text string. This one completely skips the subtitle line I posted above because it has a line break.

So essentially if I could get this one to notice the line break within the single subtitle line itself it would work, but I don't really understand what is going on here.

Thanks for any help. I wish I understood this stuff better but I am not a programmer....but I can fix your car!
Ronstang
 
Posts: 50
Joined: Tue Feb 09, 2021 2:14 am

Re: I need help with a regex problem I don't understand. Luuk?

Postby Ronstang » Tue Feb 17, 2026 10:57 am

I figured it out:

Find: (?s)^'(.*?)'$

Replace: <i>$1</i>

Google isn't always useless, but I still don't understand it!!!
Ronstang
 
Posts: 50
Joined: Tue Feb 09, 2021 2:14 am

Re: I need help with a regex problem I don't understand. Luuk?

Postby Ronstang » Tue Feb 17, 2026 10:57 am

I figured it out:

Find: (?s)^'(.*?)'$

Replace: <i>$1</i>

Google isn't always useless, but I still don't understand it!!!
Ronstang
 
Posts: 50
Joined: Tue Feb 09, 2021 2:14 am

Regex (?s)

Postby Luuk » Wed Feb 18, 2026 7:52 am

(?s)^'(.*?)'$
The (?s) tells some versions of regex to let . match newline characters.
This lets (.*?)'$ span many lines until finding ' at some EOL.

To also match whitespace, or the final ' just before some <tag>, could edit like...
(?s)^\s*'(.*?)'(?=\s*($|<))

The ?= is just to prevent from using $2 in the replacement, in case of < getting matched.
But if wanting to keep beginning/ending whitespace, would need to regular-group it instead.
Luuk
 
Posts: 849
Joined: Fri Feb 21, 2020 10:58 pm

Re: I need help with a regex problem I don't understand. Luuk?

Postby Ronstang » Wed Mar 18, 2026 2:52 am

Thanks Luuk, I do always appreciate your willingness to help. I apologize about not responding sooner but this post completely slipped my mind.

I do not really understand what you are saying but I tried your option and it gave similar results to what I had already posted that worked. My question is there a benefit to using your suggestion? Will it cover things the other may miss? Is is still the same replace of $1?

Now this morning I found something interesting and if the line in the sub is a dialog with the hyphens in front then the single apostrophes are left alone. Some examples:

- Hello.
- 'How are you, squirt?'

- Alright.
- 'What are you doin'?'

- Playing.
- 'Where's mummy?'

- Yes.
- 'What?'

In each of the above cases where there is a dialog of two lines each beginning with a hyphen the apostrophes at the beginning and end of the line behind the hyphen are ignored and not changed to the italics flags.

Do you have a solution for this problem? Today is the first I have become aware of it and I simply do not know what to do here.

Thanks
Ronstang
 
Posts: 50
Joined: Tue Feb 09, 2021 2:14 am

Regex (?s)

Postby Luuk » Wed Mar 18, 2026 6:00 pm

Your original solution matches the 1st ' directly at BOL, and the next-one at any next EOL.
The enhanced-one just allows for whitespace at BOL, but also whitepace at either EOL or "<".
Since this forum destroys spaces at BOL, Im using ^|$ to illustrate BOL|EOL in some examples.

Single-lines with original solution
^'Text'$ -----------> <i>Text</i>
^ 'Text'$ ----------> (not edited)
^'Text' $ ----------> (not edited)
^'Text'<tag>$ ----> (not edited)
^ 'Text' <tag>$ ---> (not edited)

In all cases, both ' are matched with the enhanced-solution, and the same logic applies to multiple-lines.
The enhanced-solution never needs $2 (this is why a lookaround was used, instead of regular-grouping).
But the enhanced-one does NOT grant for allowing any hyphens at BOL before that 1st-apostrophe!

It could easily be edited for this, but unfortunately, its unclear if you want to keep this extra text?
So I'm just going to regular-group it, so you can choose to either use/omit $1 in the replacement...

(?s)^([-\s]*)'(.*?)'(?=\s*($|<))
$1 matches the extra-text (if you consider hypens|spaces at BOL extra)?
$2 matches what must be kept inside of your replacement.
$3 isnt needed, that group is still just a (?=lookahead).
Luuk
 
Posts: 849
Joined: Fri Feb 21, 2020 10:58 pm

Re: I need help with a regex problem I don't understand. Luuk?

Postby Ronstang » Wed Mar 18, 2026 7:49 pm

Thanks Luuk, I just tried what you posted. Using a replace of $1 in the second line of each I just get the hyphen in italics. If I use $2 as the replace I get the text within the italics on the second line but the hyphen is gone now. Considering this is dialog I would like to keep the hyphen in the second line also. Is there a way to do this be editing you provided solution?

Thank you.
Ronstang
 
Posts: 50
Joined: Tue Feb 09, 2021 2:14 am

Combining groups

Postby Luuk » Thu Mar 19, 2026 12:28 am

The match doesnt need to change from the last-one just posted.
To keep the hyphen (without italicizing it), the replacement is like...
$1<i>$2</i>

To keep the hyphen and italicize it, its more like...
<i>$1$2</i>
Luuk
 
Posts: 849
Joined: Fri Feb 21, 2020 10:58 pm


Return to How-To