Τρόπος χρήσης του RegEx στο Microsoft Word - Συμβουλές Excel

Η Λίσα ρωτά:

Υπάρχει τρόπος να αλλάξετε έναν αριθμό (πάντα ένας τυχαίος αριθμός) μετά τη λέξη αλεπού; Παράδειγμα: αλεπού 23, αρκούδα 1, αλεπού 398, βάτραχος 12, αλεπού 15. Θέλω να αλλάξω τον αριθμό στο ίδιο χρώμα της λέξης αλεπού.

Μπορούμε να βρούμε και να αντικαταστήσουμε με μορφή στο Microsoft Word. Αυτή είναι μια εξαιρετική δυνατότητα για γρήγορη εύρεση του μορφοποιημένου κειμένου και ακόμη και αντικατάσταση ολόκληρης της μορφής κειμένου στο έγγραφο.

Επιλέξτε Σύνθετη εύρεση στην κορδέλα.

Εύρεση και αντικατάσταση διαλόγου

Εισαγάγετε το κείμενο για εύρεση και, στη συνέχεια, κάντε κλικ στο κουμπί Περισσότερα για να δείτε σύνθετες επιλογές και κάντε κλικ στο κουμπί Μορφοποίηση.

Σύνθετες επιλογές εύρεσης

Επιλέξτε την επιλογή Γραμματοσειρά στις ρυθμίσεις και, στη συνέχεια, μπορείτε να ρυθμίσετε το χρώμα κειμένου που θέλετε να βρείτε στο έγγραφο. Κάντε κλικ στο OK για να κλείσετε το παράθυρο διαλόγου Εύρεση γραμματοσειράς.

Επιλέξτε χρώμα κειμένου στο παράθυρο διαλόγου Εύρεση γραμματοσειράς.

Κάντε κλικ στο Find Next και θα δείτε την πρώτη εμφάνιση του κειμένου που αναζητάτε σε συγκεκριμένο χρώμα.

Βρείτε Επόμενο για να βρείτε την πρώτη εμφάνιση.

Μπορούμε επίσης να κάνουμε πιο περίπλοκες αναζητήσεις χρησιμοποιώντας μπαλαντέρ. Ωστόσο, η εγγενής ενότητα αναζήτησης του Word δεν μας αφήνει να κάνουμε μια αναζήτηση όπως ζήτησε η Λίσα.

Εκεί μπορούμε να καλέσουμε το RegEx στο παιχνίδι!

Βιβλιοθήκη VBSCript Regular Expressions

Το VBA δεν αποστέλλεται με κανονική υποστήριξη έκφρασης. Ωστόσο, η βιβλιοθήκη Microsoft VBScript περιέχει ισχυρές δυνατότητες κανονικής έκφρασης. Αυτή η βιβλιοθήκη είναι μέρος του Internet Explorer 5.5 και νεότερη έκδοση, επομένως είναι διαθέσιμη σε όλους τους υπολογιστές που χρησιμοποιούν Windows XP, Vista, 7, 8, 8.1 ή 10.

Χρήστες Mac

Δεδομένου ότι ο Internet Explorer δεν είναι εφαρμογή Mac, αυτή η βιβλιοθήκη δεν υπάρχει σε Mac. Επομένως, τα παρακάτω δείγματα VBA δεν λειτουργούν σε Mac.

Για να χρησιμοποιήσετε αυτήν τη βιβλιοθήκη στο VBA, μεταβείτε στο VBE, επιλέξτε Project and References στο μενού VBE και, στη συνέχεια, μετακινηθείτε προς τα κάτω στη λίστα για να βρείτε το στοιχείο "Microsoft VBScript Regular Expressions 5.5" και σημειώστε το για να το συμπεριλάβετε στην εφαρμογή.

Βιβλιοθήκη τακτικών εκφράσεων VBScript

Εισαγάγετε μια νέα ενότητα και αντιγράψτε και επικολλήστε τον ακόλουθο κώδικα σε αυτήν την ενότητα.

Sub doRegexFind() Dim strSample As String Dim objRegex As RegExp Dim matches As MatchCollection Dim fnd As Match strSample = "First product code is fox 12, second one is fox 22, and third product is fox 45." Set objRegex = New RegExp With objRegex .Pattern = "fox d+" .Global = True .IgnoreCase = True Set matches = .Execute(strSample) For Each fnd In matches Debug.Print fnd Next fnd End With End Sub

Αυτή η διαδικασία παίρνει το δείγμα κειμένου, βρίσκει τους κωδικούς προϊόντος με το δεδομένο μοτίβο - το οποίο ξεκινά με "fox", μονό διάστημα και έναν αριθμό και εκτυπώνει τους αντίστοιχους κωδικούς στο παράθυρο Άμεση (πατήστε Ctrl + G στο VBE εάν δεν ήδη ορατό).

Αντιστοιχισμένοι κωδικοί προϊόντος που εκτυπώνονται στο παράθυρο Άμεση.

d+ Η κλάση χαρακτήρων στο μοτίβο ορίζει έναν ή περισσότερους αριθμητικούς χαρακτήρες και το μοτίβο είναι βασικά πρόθεμα "fox" ακολουθούμενο από ένα κενό διάστημα που ακολουθείται από αριθμούς.

Περισσότερες πληροφορίες

Επισκεφτείτε τη γλώσσα Κανονικής έκφρασης - Γρήγορη αναφορά για περισσότερες πληροφορίες σχετικά με τις διαφυγές χαρακτήρων, τις κατηγορίες χαρακτήρων και τις άγκυρες.

Αντιγράψτε και επικολλήστε τον ακόλουθο κώδικα για να δείτε το RegEx σε δράση για να αφαιρέσετε τα κενά από τους κωδικούς προϊόντων.

Sub doRegexFindReplace() Dim objRegex As RegExp Dim matches As MatchCollection Dim fnd As Match Dim strSample As String strSample = "First product code is fox 12, second one is fox 22, and third product is fox 45." Set objRegex = New RegExp With objRegex .Pattern = "(fox) (d+)" .Global = True .IgnoreCase = True strSample = .Replace(strSample, "$1$2") End With Debug.Print strSample End Sub

This procedure replaces the sample text content by removing the spaces from the product codes matched with the given pattern, and prints the result text in the Immediate window.

Replaced text printed in the Immediate window.

Please note that pattern is slightly different than the first code. Terms in this pattern are enclosed with parentheses, and corresponding terms are used in the Replace method as $1 and $2 in order. This procedure simply joins the two terms without spaces.

Back to the Question

Let's go back to the sample text we used at the beginning of this article.

Sample Text

We need to find "fox" followed by numeric characters, and change the match by using the color of the "fox" section in the matched text.

Although RegEx is very good matching by the given pattern, it cannot replace the color of text in Word document. So we will combine RegEx and Word VBA methods in the following procedure.

Here are the steps:

  1. Find the matches with RegEx.
  2. Search each matched text by using Word Find method.
  3. Find the color of the first word in the found range.
  4. Change the color of the found range with the color in the previous step.

Switch to VBE, and insert a new module. Make sure VBScript Regular Expressions library is added to the project, and copy and paste the following code into this new module.

Sub doRegexMagic() Dim str As String Dim objRegex As RegExp Dim matches As MatchCollection Dim fnd As Match Set objRegex = New RegExp str = "fox" With Selection .HomeKey wdStory .WholeStory End With With objRegex .Pattern = str & " d+" .Global = True .IgnoreCase = True Set matches = .Execute(Selection.Text) End With With Selection .HomeKey wdStory With .Find .ClearFormatting .Forward = True .Format = False .MatchCase = True For Each fnd In matches .Text = fnd .Execute With Selection .Font.Fill.ForeColor = .Range.Words(1).Font.TextColor .MoveRight wdCharacter End With Next fnd End With .HomeKey wdStory End With End Sub 

Run the code, and here is the result.

Result

Download Word File

To download the Word file: how-to-use-regex-in-microsoft-word.docm

RegEx in Excel?

Regex is completely missing from Excel. However, we can still use VBScript Regular Expressions in Excel VBA.

Launch Excel, open a new workbook, and create the content as shown below.

Sample data in Excel

Reference

This article has been inspired by Learn Excel 2010 - "Find & Replace Color of A Certain Word": Podcast #1714 published by Bill Jelen on May 21, 2013! So we wanted to use similar sample text as he used in the video. We just added numbers after the "fox".

Switch to VBE, and insert a new module. Make sure VBScript Regular Expressions library is added to the project just like you did in Word, and copy and paste the following code into this new module.

Sub doRegexMagicInExcel() Dim str As String Dim objRegex As RegExp Dim matches As MatchCollection Dim fnd As Match Dim rng As Range Dim cll As Range Set objRegex = New RegExp Set rng = Selection str = "fox" With objRegex .Pattern = "(" & str & ") (d+)" .Global = True .IgnoreCase = True For Each cll In rng.Cells Set matches = .Execute(cll.Value) For Each fnd In matches cll.Value = .Replace(cll.Value, "$1$2") Next fnd Next cll End With End Sub

Return to worksheet, and select the range with sample text. Run the macro, and see the result.

Result in Excel

This procedure loops through the cells in the selected range, replaces the text in the cells by removing the spaces from the product codes matched with the given RegEx pattern.

Download Excel File

To download the Excel file: how-to-use-regex-in-microsoft-excel.xlsm

ενδιαφέροντα άρθρα...