Monday, August 23, 2021

Tip of the Day: Using Regular Expressions for Finding Text
Product.......: R:BASE X.5 and R:BASE X.5 Enterprise (Version 10.5)
Build.........: 10.5.3.20812 or higher
Sections......: R:BASE Editor, Trace Debugger
Keywords......: Find, Replace, Text, Regular Expressions

Did you know that you can use regular expressions for finding text in command files?

The "Find Text" and "Replace Text" dialogs allow users to locate text in a command file, Custom EEP, Custom Form Action, and also within the Trace Debugger.

Search criteria can be modified based on case sensitivity, whole word searches, and regular expressions. The search direction, scope, and origin may also be specified.

Regular expressions consist of patterns that can be used to search for variable forms of text. Without regular expressions, you must know the exact phrase for which to search; regular expressions allow you to find all text that follows a certain pattern.

Syntax:
Regular expressions generally consist of some constant text, and some restrictions or wildcards on the rest of the text. If you want to use one of these operators as literal text, you must precede the character with a backslash.

The following is a list of regular expressions used only for searching text. When searching text, you can also use regular expressions that are used for all types of operations.

Char    Meaning
\ Escape character. If you want to use the character '\' itself, you should use '\\'.
  ^     Line start
  $     Line end
* Character to the left of the asterisk in the expression should match 0 or more times.
          For example, 'be*' matches 'b', 'be' and 'bee'.
+ Character to the left of the plus sign in the expression should match 1 or more times.
          For example, 'be+' matches 'be' and 'bee' but not 'b'.
  .     Matches any character.
  [ ]   Any of the enclosed characters may match the target character.
          For example, '[ab]' matches 'a' and 'b'. '[0-9]' matches any digit.
  [^]   None of the enclosed characters may match the target character.
For example, '[^ab]' matches all characters except 'a' and 'b'. '[^0-9]' matches any non-digit character.
  -     When used within a character class, represents a character range.
{} Create a group. E.g. "{[0-9][0-9]}" is a group that is composed of 2 digits. The search string "{[0-9][0-9]}+" will find the first 8 digits in "123456789" because that is 4 pairs of 2 digits.

The below examples can be tested in the R:BASE Editor. The "Search sample and result" items are added to a command file and the "Search criteria" is used to find the text, with the "regular expression" check box enabled.

-----------------------------
Example 01:
Find a line that starts with "A" and ends with "2":

Search criteria: ^A.*2$
?^A - Line must start with an A
?.*? - Zero or more characters (any character)
?2$? - Line must end with a 2

Search sample and result:
Abc-12 - Found
A2 - Found
1Abc-12 - Not found (line does not start with A)
Abc-123 - Not found (line does not end with 2)

-----------------------------
Example 02:
Find text that contains the word "Plan," followed by optional space and a 4-digit number:

Search criteria: Plan *[0-9][0-9][0-9][0-9]
?Plan - The literal word 'Plan' must start the phrase
? * - (space character followed by *) Zero or more space characters
?:d:d:d:d - Any four digits

Search sample and result:
My Plan1234 - Found
Plan 1234 - Found
My Plan 123a - Not found (only 3 digits follow Plan)

-----------------------------
Example 03:
Find text that contains the characters ' [# ' but is not followed by a 1, 2, 3, 4, or B:

Search criteria: \[#[^1-4B]
?\[ - The literal character [ (must be escaped)
?# - The literal character # (not an operator, does not need to be escaped)
?[^? - Starts a character class, meaning any character but those described in the class
?1-4B - Range 1-4 (1, 2, 3, 4) and B
?]? - Ends the character class

Search sample and result:
Part [#9778C] - Found
Lot [#554] - Found
Part #977 - Not found (# not preceded by [)
Part [#155A] - Not found (# followed by 1)

-----------------------------
Example 04:
Find a line that starts with "Detail," followed by one or more spaces, followed by one or more alphanumeric characters, and a Z:

Search criteria: ^Detail +[A-Z0-9]+Z
?^Detail? - Line must start with Detail
 +? - (space character followed by +) One or more space characters
[A-Z0-9]+ - One or more ASCII alphanumeric characters
?Z - The literal character Z

Search sample and result:
Detail 143Z - Found
Detail AABZ - Found
Plot A Detail 3Z - Not Found (line does not start with Detail)

-----------------------------
Example 05:
Find currency values in the form $#,###.## :

Search criteria: \$[0-9,]+\.[0-9][0-9]
\$ - Match must start with a dollar sign
[0-9,]+ - Matches one or more digits or commas
\. - Followed by a decimal point
[0-9][0-9] - Followed by exactly two digits

Search sample and result:
$12.34 - Found
$12,123.00 - Found
12,435.00 - Not found (does not start with $)
$12 - Not found (does not end with a decimal point and two digits)


Very Best R:egards,

Razzak.

https://www.rbase.com
http://www.facebook.com/rbase/

--
For group guidelines, visit 
http://www.rbase.com/support/usersgroup_guidelines.php
--- You received this message because you are subscribed to the Google Groups "RBASE-L" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rbase-l/0MNaM0-1mOB310QmI-007Axc%40mrelay.perfora.net.

Reply via email to