0

Pattern Match

  • updated 9 days ago

Description

The Find Pattern part allows you to find patterns within text, and it provides start and end positions in a text string for any specified pattern. 

Node Type: This part is located in process nodes under the Text manipulation dropdown menu.

Used With: This part can be used in applications without frameworks as well as in conjunction with all LogicNets frameworks.

Use

This part is most often used in retrieving partial codes or strings from a longer code; for example, extracting a subset of characters from a product part number. Since this part uses Lua pattern-matching syntax, you can define sophisticated matching, even when the input string is not completely defined or has multiple variations because of time/source.

Pattern matching also allows you to validate text/numeric inputs that have to match a specific structure.

Tips & Tricks

When using this part, the syntax for matching the text exactly can be tricky. Consider setting up a small test logicnet where you can verify your pattern match outside of the larger application. It can also be helpful to start with an easy pattern match (e.g. extracting a number) and then add complexity from there, testing at each step to insure your pattern is correct.

In current installations (up to v8.0) the part does NOT reset when encountering an error, and will present any prior run answer. To counter this, make sure to delete the result object prior to running the part. This will insure that in event of an error the output object will be null.

Editor Fields

Field Name Description/Use Type/Options  Optional/Mandatory 
Input value This is where you enter the name of the data object containing the source string. Use $() around the data object name. Text Mandatory
Pattern Use this field to specify a pattern. 
Put the part of the pattern to be extracted to the found string inside parentheses.
This can be explicit or can use the Lua pattern-matching syntax, e.g.
(ABC)  / Matches:        ABC, AAABCCC
         Non-matches:    ABBCC, AB, CBA
^(%d)$ / Matches:        8, 1, 0
         Non-matches:    12, A1, 1C


See below for more information on Lua pattern matching.
Text | Data Object Mandatory
Start position stored to This field stores the position in the string of the first occurrence and first character of any match. Data Object Optional
End position stored to This field stores the position in the string of the first occurrence and last character of any match. Data Object Optional
Found string stored to This is the path/variable name where the system saves the matched string for downstream processing. Data Object Optional
Regular Expression Checking this box allows the system to use Lua v4 regular expression syntax for the match and replacing strings.

See Lua 4.0 Reference Manual, Gsub section for more information.
On | Off

Defaults to ON for all Releases prior to v7.4.3.
Optional 

Pattern Matching

The following table contains the codes to use for pattern matching.

Code Definition Explanation
. All Characters Using this code in a pattern allows for any character.
%a Letters This pattern code excludes all non-letter characters, such as ! @ , + etc.
%c Control Characters Control Characters (\n, \t, \r, ...)
%d Digits Digits (0-9)
%l Lowercase Letters Lowercase Letter (a-z)
%p Punctuation characters Punctuation Characters (!, ?, &, ...)
%s Space Characters Space Characters
%u Uppercase Letters Uppercase Letters
%w Alphanumeric Characters Alphanumeric Characters (A-Z, a-z, 0-9)
%x Hexadecimal Digits Hexadecimal Digits (\3, \4, ...)
%z The character with representation 0
%A,%D etc. Complement of above An uppercase version of any of those classes represents the complement of the class; for instance, '%A' represents all non-letter characters. Example: match("f123", "%D") --> f

Magic Characters

% Acts as escape for any other magic character This character acts as an escape for any other magic character; for example, '%.' matches a dot and '%%' matches the character '%' itself.
[ ] Character Set char-set allows you to create your own character classes, combining different classes and single characters between square brackets. For instance, the char-set '[%w_]' matches both alphanumeric characters and underscores. The char-set '[01]' matches binary digits. 

You can also include character ranges in a char-set, by writing the first and the last characters of the range separated by a hyphen. You will seldom need this facility, because most useful ranges are already predefined; for instance, '[0-9]' is simpler when written as '%d'.

Example:  If foo = "bar123bar2341" then match "[arb]" --> b
+ One or more repetitions (longest) The '+' modifier matches one or more characters of the original class, and it will always get the longest sequence that matches the pattern. For instance, the pattern '%a+' means one or more letters, or a word; the pattern '%d+' matches one or more digits (an integer).

Example: If foo = "12345678bar123" then match "%d+" --> 12345678
* Zero or more occurrences The modifier '*' is similar to '+', but it also accepts zero occurrences of characters of the class. The pattern '%s*' matches zero or more spaces.
- One or more repetitions (shortest) Like '*', the modifier '-' also matches zero or more occurrences of characters of the original class. However, instead of matching the longest sequence, it matches the shortest one.
? Optional Character The modifier, '?', matches an optional character. As an example, suppose we want to find an integer in text where the number may contain an optional sign. We can use the pattern '[+-]?%d+', matching numerals like "-12""23"and "+1009".
^ Match from beginning If a pattern begins with a '^', it will match only at the beginning of the subject string.
$ Match at end If a pattern ends with a  '$', it will match only at the end of the subject string.

To read the Lua manual on pattern matching, click Lua Manual - Patterns.

Note that the Regex Library can help you identify an approach for obtaining a specific pattern match, but Lua does not have the full range of REGEX expressions available. This means that some of the more complex solutions in Regex Library are not compatible with LogicNets; this includes the | (alternation) or {...} (explicit quantifier) notations.

Validation in Form Parts

Use the following syntax in the validation field for a text input that has to match a specific pattern:

strfind ( ':val:' , 'pattern-string' )

:val is equivalent to using $(data_object) and references the input value.  pattern-string is the characters built using the codes above.

As an example, the following image displays the pattern match syntax for verifying that the text input matches the pattern A-123.

Example

Download and import the following example project in your workspace. 

Reply Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
Like Follow
  • 2 yrs agoLast active
  • 146Views
  • 4 Following

Home