[Solved] Regular expression: capitalized words EXCEPT some

Discuss the word processor
Post Reply
davidcannon
Posts: 3
Joined: Mon Sep 06, 2021 2:04 pm

[Solved] Regular expression: capitalized words EXCEPT some

Post by davidcannon »

I've been trying to write a macro to find all the CAPITALIZED words in a text.

This is my code :

args1(10).Value = "[:space:][:upper:][:alpha:]+[:space:]|[:space:][:upper:][:alpha:]+[:print:]|\<[:upper:][:alpha:]+[:space:]"

It works perfectly.

HOWEVER, it doesn't work if I add this to it : args1(10).Value = "[:space:][:upper:][:alpha:]+[:space:]|[:space:][:upper:][:alpha:]+[:print:]|\<[:upper:][:alpha:]+[:space:]|[^John|^Mary]" — I want to find all the capitalized words EXCEPT "John" and "Mary" (the text I'm working with has those names ad nauseum and they clutter my search so I want to exclude them — but my script doesn't work).

Can anybody please help me?

Thank you so much!
Last edited by Hagar Delest on Mon Sep 06, 2021 8:06 pm, edited 2 times in total.
Reason: Tagged [Solved].
OpenOffice 4.1.10 on Windows 10
John_Ha
Volunteer
Posts: 9583
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Trying to write a macro

Post by John_Ha »

args1(10).Value = "[:space:][:upper:][:alpha:]+[:space:]|[:space:][:upper:][:alpha:]+[:print:]|\<[:upper:][:alpha:]+[:space:]|[^John|^Mary]"

As a first thought, each " | " represents OR but the final " | " needs to be AND giving "(any capitalised word) AND NOT (John OR Mary). Or should John be JOHN?

Or do a workaround hack. First convert all John to J*** and all Mary to M***. Now search. Now convert them back.
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
FJCC
Moderator
Posts: 9231
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Trying to write a macro

Post by FJCC »

The following regular expression seems to work in the standard Find & Replace dialog.

Code: Select all

(?!\bJohn|\bMary)\b[:upper:][:alpha:]*
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.
davidcannon
Posts: 3
Joined: Mon Sep 06, 2021 2:04 pm

Re: Trying to write a macro

Post by davidcannon »

Do you mean : args1(10).Value = "[:space:][:upper:][:alpha:]+[:space:]|[:space:][:upper:][:alpha:]+[:print:]|\<[:upper:][:alpha:]+[:space:] AND NOT [John] OR Mary" — is that what you mean? I've just tried that and it doesn't seem to be working.
OpenOffice 4.1.10 on Windows 10
davidcannon
Posts: 3
Joined: Mon Sep 06, 2021 2:04 pm

Re: Trying to write a macro

Post by davidcannon »

Replying to FJCC : Wow, you've solved it for me! Much simpler code than mine, too! THANK YOU SO MUCH! BTW, do you mind telling me what the exclamation mark in your code means? (I haven't seen come across that yet in the haphazard study I've been doing). Once again, thank you!

David
OpenOffice 4.1.10 on Windows 10
FJCC
Moderator
Posts: 9231
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Trying to write a macro

Post by FJCC »

The ((?! ...) is a negative look ahead assertion. It looks ahead of the current cursor position and returns TRUE if the text encountered does not match the text in the parentheses. It is part of a family of such operators that look forward or backward and are either positive or negative. You can read about them in the Regular Expression Operators section here.
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.
Post Reply