**Harnessing the Power of Regular Expressions in Google Docs** (1 Viewer)

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 21:07
Joined
Jul 9, 2003
Messages
16,285
I have just used regular expressions (regex for short)! This is the second time I can recall using regular expressions, the first time was to vet emails. Did you know you can use regular expressions in the find box of the find and replace facility in a Google document? I copy my chat GPT chats and save them in Google Drive. Chat GPT will not follow my simple instruction, to call me "Uncle Gizmo," and insists on calling me "You".. I decided to change it & ran the Google document find and replace, and it indeed found many "YOU's," some of them in YOUR and other similar places. There were 400 of them!

I changed the first hundred and then realized it was a pointless task. However, Google piqued my interest because there, within the find & replace pop-up, it had a checkbox "use regular expressions!" So what did I do? I asked ChatGPT, "Hi chatty, how can I write a regular expression that finds all the occurrences of 'you' at the beginning of a line all by itself?"

And it answered! To pinpoint exactly where "You" appears as a standalone word on a single line in your Google document. This way, we avoid messing with words like "Your" or any other spot where "You" isn't flying solo.

Here's the regex pattern you'll need:
```
^You$
```
**Breaking Down the Regex Pattern:**
- `^` asserts the start of a line, ensuring no characters precede our target.
- `You` specifies the exact word we are looking for.
- `$` asserts the end of a line, ensuring no characters follow our target.

This pattern ensures that "You" is the only word on the line from start to finish, no more, no less. This precision is what makes regex such a powerful tool for text manipulation.

**Applying the Regex in Google Docs:**
1. Hit `Ctrl + H` to open the Find and Replace tool.
2. In the "Find" box, type `^You$`.
3. In the "Replace with" box, type "Uncle Gizmo".
4. Make sure to click on "Match using regular expressions" or a similar option to enable regex search.
5. Then, click "Replace all" to make the swap everywhere it applies.

Regular expressions turned a daunting task of manual replacements into a breeze. By the end, my documents referred to me correctly as "Uncle Gizmo," just the way I wanted. Isn't technology wonderful? (Oh chatty why do you add these superfluous comments! )
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:07
Joined
Feb 28, 2001
Messages
27,218
The only comment I might offer is that the "^" and "$" as special syntax indicators do not occur in the "standard" VBA REGEX processing for the "LIKE" operator and for the REGEX functions. This is, perhaps, to be expected for tools that do not originate from the same vendor. Therefore, if you want to apply REGEX processing in whatever YOU are doing (in or not in Google Docs), I would add one AND ONLY ONE warning ... first look up the syntax of the REGEX processor you intend to use. Then Uncle G's advise is great! And of course, his advice is spot-on for Google Docs.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 21:07
Joined
Jul 9, 2003
Messages
16,285
Therefore, if you want to apply REGEX processing

Is there a similar facility, to allow find and replace with reg x in Microsoft word?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:07
Joined
Feb 28, 2001
Messages
27,218
Yes, of course. VBA's REGEX will work in any VBA macro and Word does support VBA macros... grudgingly. Google Docs doesn't use the same library as MS Office does so they have the chance (make that likelihood) to be different.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 21:07
Joined
Jul 9, 2003
Messages
16,285
Is there a similar facility, to allow find and replace with reg x in Microsoft word?

With the Google document all you do is open the find and replace dialog box and put the term you are searching for in the search box, with the regex code surrounding it:- ^You$ ... Then you put what you want to replace it within the replace box, and that's it.

It's so simple and easy! The only hassle is writing the regex code, but that's no longer hassle because ChatGPT writes it for you!

I don't recall any such simple search facility in Microsoft Word although I have not used it much lately.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:07
Joined
Feb 28, 2001
Messages
27,218
It would only appear in VBA, which IS usable in Word as a Macro. You wouldn't use it in the standard Word GUI.
 

DickyP

Member
Local time
Today, 21:07
Joined
Apr 2, 2024
Messages
38
You can use 'proper' regular expressions anywhere you like in Access VBA and queries. Just use the VBScript RE engine.

Just add a reference VBScript _regExp_55, which is done using the vbscript.dll\3. I use a function RLike() to mimic the RLike in MySQL and Oracle.


Code:
Public Function RLike(Optional pstrSource As String = vbNullString, _
                      Optional pstrPattern As String = vbNullString, _
                      Optional pfMultiLine As Boolean = False, _
                      Optional pfIgnoreCase As Boolean = True _
                      ) As Boolean
                
' =====================================================================================================
'
' This function emulates the RLIKE predicate available in MySQL and other SQL DBMSs. Although it works
' well enough, the following should be noted:
'
'   1. An error will occur if a null value is passed as either of the source, or pattern, parameters.
'   2. Because it is not 'built-in' to the JET Engine it will be slow against large recordsets.
'   3. An error will occur if an invalid Regular expression pattern is passed.
'
' The Regular Expression object is decalare as static to improve performance, by not requiring it to be
' instantiated for every row tested.
' -----------------------------------------------------------------------------------------------------
' Author:           C R J Pearson
' Last updated:     24 May 2007
' Version:          1.0
' =====================================================================================================
    If pstrSource = vbNullString Or pstrPattern = vbNullString Then
        RLike = False
        Exit Function
    End If
    
    Static RE As Object
    
    If RE Is Nothing Then
        Set RE = CreateObject("VBScript.RegExp")
    End If
    
    With RE
        .Pattern = Trim$(pstrPattern)
        .Multiline = pfMultiLine
        .IgnoreCase = pfIgnoreCase
        RLike = RE.Test(pstrSource)
    End With
End Function

Hope this may help,
 

sxschech

Registered User.
Local time
Today, 13:07
Joined
Mar 2, 2010
Messages
793
With the Google document all you do is open the find and replace dialog box and put the term you are searching for in the search box, with the regex code surrounding it:- ^You$ ... Then you put what you want to replace it within the replace box, and that's it.

It's so simple and easy! The only hassle is writing the regex code, but that's no longer hassle because ChatGPT writes it for you!

I don't recall any such simple search facility in Microsoft Word although I have not used it much lately.

Yes, you can use the Find and Replace dialog box within word (ctrl+H) and do regex type find and replace. No need for vba or macros.

Here is an intro to the steps with screen shot

 

DickyP

Member
Local time
Today, 21:07
Joined
Apr 2, 2024
Messages
38
Obviously it precedes Google Docs so it is no answer to the original post of this thread, but if you can get hold of it anywhere I strongly recommend the book 'Beginning Regular Expressions' by Andrew Watt (ISBN: 9780764 574894) published by Wrox in in 2005. It is especially good on the Word RE usage.
 

Users who are viewing this thread

Top Bottom