Using regex for an optional string

Great stuff here!

I’m fairly decent with basic Regex, but where I struggle with is with an example like below

===
Submission 1
Name: Bryan
Email: test@abc.com
Phone: 012345

===
Submission 2
Name:Bryan2
Phone: 0141231

Normally what I would have done is used a Regex pattern of

Name:(.)\nEmail:(.)\nPhone:(.*)

Which works great for Submission 1, but not Submission 2. Is there a way in regex to treat a string as optional? I.e I’d like Email:(.*) to be an optional match. i.e if not found just skip straight to the next possible match.

@D-EFFCON

Here’s one way:
Name:(.*?)\n(Email:(.*?)\n|)Phone:(.*)


Jim - The Monday Man (YouTube Channel)
Watch Our Latest Video: DYNAMIC GROUPING - The Most Powerful monday Feature Ever? - YouTube
Contact me directly here: Contact – The Monday Man

((Corrected))

1 Like

Hi Jim,

Gave that a try, but didn’t seem to work

It didn’t match any of the text in the string

This seemed to work for me

Name:(.)\n(Email:(.)\n|)Phone:(.*)

image

Thanks for pointing me in the right direction Jim!

EDIT: I just realized that Discourse is hiding the * in the first two group brackets. You can notice a difference between the text I pasted and the screenshot

@D-EFFCON

My bad.

I’m usually very good at making sure to use preformatted text for that. :frowning:

My original text:

Name:(.*?)\n(Email:(.*?)\n|)Phone:(.*)

A better version:

Name:\s*(.*?)\n(Email:\s*(.*?)\n|)Phone:\s*(.*)
3 Likes

When putting in code excursions try to escape any expressions with the code tool.

Code expression

4 Likes