• Documentation
  • Workato Blog
  • Product Blog
  • Return to Workato
  • Login
  • Automation Institute
  • Documentation
  • Community Forums
New Topic
Discussions Recipe Building Formula Mode Questions

Adam Goh

started a topic over 7 years ago

Regular Expression (Regex) and when to use it

Good day all!


Today we will tread to uncharted territories for many users who have 0 coding experience. Workato requires 0 coding as stated, but in order to have magical powers, a wizard needs to be called, and the wizard we have in Workato is Regular Expression


Warning: Regular Expression is an advanced tutorial that may need prior pattern/formula matching experience.


What is Regular Expression (Regex)

A quick tutorial to regular expression that might help you get started

http://www.regular-expressions.info/quickstart.html


Regex is a sequence of symbols and characters expressing a string or pattern to be searched for within a longer piece of text. You may use it to extract a US phone number, an address, or any information that follows a constant pattern in the body of text. 


Basics of Regular Expressions (Regex)

abc….....Letters

123….....Digits

\d    .......Any Digit

\D   ........Any Non-digit character

.      ........Any Character

\.     ........Period

[abc].......Only a, b, or c

[^abc].....Not a, b, nor c

[a-z]........Characters a to z

[0-9]........Numbers 0 to 9

\w...........Any Alphanumeric character

\W..........Any Non-alphanumeric character

{m}.........m Repetitions

{m,n}......m to n Repetitions

*..............Zero or more repetitions

+.............One or more repetitions

?.............Optional character

\s............Any Whitespace

\S............Any Non-whitespace character

^…$........Starts and ends

(…)..........Capture Group

(a(bc))......Capture Sub-group

(.*)............Capture all

(x|y)..Matches x or y


Modifiers

i

Ignore case when matching text.


o

Execute only once for any #{} substitutions in a particular regex literal, that is during its first time. Otherwise, the substitution will be performed overtime the literal generates a Regex object.


x

Ignores whitespace and allows comments in regular expressions


m

Matches multiple lines, recognizing new lines as normal characters


u,e,s,n

Interpret the regexp as Unicode (UTF-8), EUC, SJIS, or ASCII. If none of these modifiers are specified, the regular expression is assumed to use the source encoding.



What can you do with Regular Expressions (Regex)?

Regex is a text formatting tool. Whenever you find yourself needing to extract a certain pattern from your text, regex is the best way to go. To use Regex, make sure that formula mode is turned on for that field.


To get started with Regex, first, your field must be in formula mode. Second, have your regex in the function of .split(). An example of how using regex to get my name out of my emails is




When should I not use Regex?

When the text that you want to extract do not follow a rigid, predictable pattern. For example, extract numbers out from a body of text will not guarantee that you will always extract the telephone number. 


Regex Use cases

1) Getting Email address from body of text

[Data pill].split(/\w+@[\w.-]+|\{(?:\w+, *)+\w+\}@[\w.-]+/i)

This extracts the first email that it sees in that data pill. However, do note that if there is any @ in the text that does not belong to an email, it will extract that section of text instead of the email.


Here's an explanation extracted from regex101.com

  

/\w+@[\w.-]+|\{(?:\w+, *)+\w+\}@[\w.-]+/i
 
1st Alternative: \w+@[\w.-]+
 
\w+ match any word character [a-zA-Z0-9_]
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
@ matches the character @ literally
 
[\w.-]+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\w match any word character [a-zA-Z0-9_]
.- a single character in the list .- literally
 
2nd Alternative: \{(?:\w+, *)+\w+\}@[\w.-]+
\{ matches the character { literally
 
(?:\w+, *)+ Non-capturing group
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
 
\w+ match any word character [a-zA-Z0-9_]
Quantifier: + Between one and unlimitedtimes, as many times as possible, giving back as needed [greedy]
, matches the character , literally
 
* matches the character literally
Quantifier: * Between zero and unlimitedtimes, as many times as possible, giving back as needed [greedy]
 
\w+ match any word character [a-zA-Z0-9_]
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\} matches the character } literally
@ matches the character @ literally
 
[\w.-]+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\w match any word character [a-zA-Z0-9_]
.- a single character in the list .- literally
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

  

2) Getting a 15 digit ID that starts with =?003

[Datapill].split(/\b(?=003\B{12})/).last[0,15]



How do I test my Regex to know if it'll give me the right result without wasting any transaction?

https://regex101.com provides an online regex tester and debugger, with information onto the kind of format that you are using to help you test your regex if you will be able to extract the right pattern out as you intended. Do note that in your text, remember to have several scenarios to make sure your regex is robust. 


Here is how it looks like:




Reference

https://regex101.com

http://www.regular-expressions.info/quickstart.html

http://regexone.com

P

Paul Hutcherson

said about 6 years ago
Thank you.  However, there is no discussion of what the value that is returned in a string type looks like.  I understand the example of the email, but when we pass something relatively simple, it's ignoring the regex filter we provided and returning the wrong data.  Please provide more detail about what is returned within the output and how to validate that you've written the regex correctly. 

 


1 person likes this
Login to post a comment

Still can't find your solution?

Send us a ticket, we will try our best to assist you with your problem

Documentation
Developer's Library
Tutorials
eBooks
Product Hour
Product Blog
Workato Blog
Product updates
Customer Stories
© Workato 2022   Privacy   Terms   +1 (844) 469-6752
  • Documentation
  • Workato Blog
  • Product Blog
  • Solutions
  • Forums
  • Tickets
  • Log in
  • Return to Workato
Topic views count