Introduction
This short article is about how to use regex with Dataweave 2.0 with regard to email validation. Regex is used in Mulesoft language very wide with regard two functions, matchs(...) and match(...). It is very important to master the regular expression in order to be professional in Mulesoft integration projects. The are a lot of reference available one. Here are few:Use Case
We expect the output of the dataweave transformation as the following depending on the validity of the email address:1 2 3 4 5 6 7 8 9 10 11 12 13 | [ { ... "invalidEmail" : "johndo@yahoo" ... }, { ... "PersonEmail" : "john.smith@google.com" ... }, ... ] |
Invalid Emails
The following types of emails are invalid:- beginning with a dot: .gary.liu@google.com
- ending with a dot: gary.liu@google.com.
- double dots: gary.liu@google..com
- domain name contains underscore: gary.liu@att_rr.com
- domain name contains space: gayr.liu@att rr.com
- domain name contains and of the following: ,<>/[]
- no organization email: gary@google
Solution
1 2 3 4 5 6 7 8 | %dw 2.0 output application/json var regexEmail = /^[^.][a-zA-Z0-9.! #$%&’*+\/=?^_`{|}~-]+@[a-zA-Z0-9-](?!.*?\.\.)[^_ ; ,<>\/\\]+(?:\.[a-zA-Z0-9-]+)[^.]*$/ --- payload map using (email = $.email) { (validEmail: email) if (email matches regexEmail), (invalidEmail: email) if ( not (email matches regexEmail)) } |
- negation: [^_;,\.] this expression means if the email domain contains underscore _ , semi-coma, etc. is not valid email
- simple ^ and $ represent the beginning and end of the line
- [a-zA-Z0-9] mean any charater A a, Bb ... Zz, or 0 to 9 digits are valid
- + sign means to match one for more
- * sign matches 0 or more
- ?! means not include, (?!.*?\.\.) --> not include double dots: ..
Very informative post for mulesoft developers.You can also visit goformule.com for mulesoft stuff.
ReplyDeleteGlad I discovered this on google .
ReplyDeleteYou have shared amazing post. This post is really helpful for us to know the information. Thank you for taking your time to post such a wonderful article.Get for more information.
Free Email Validation | Check Valid Email Address
ReplyDeleteI like your writing style really loving this website .
Email Validation Tool
Thanks for sharing most valuable information.
ReplyDeleteMulesoft Training Hyderabad
Best Mulesoft Online Training
Very nice post
ReplyDeleteMulesoft Online Training
Mulesoft Online Training in Hyderabad
Great explanation of real-time email verification! It’s a powerful way to ensure that only valid contacts enter your system from the start. At ZUHAL, we offer both real-time and bulk email verification solutions to help businesses reduce bounce rates and maintain high deliverability. Preventing bad emails at the source really makes a difference in long-term email success. Thanks for the informative post!
ReplyDelete