Regex for extracting text from .eml file - regex

I need to write regex to get the following data from an email. The data to be phrased is first name, last name, phone number, email id, pin code, message etc, i am a newbie and am not much aware of REGEX, can anyone help me with it.
enter code hereContact Us
Title
Mr.
Last Name
S
First Name
Nitesh
Contact Us
By phone on:
0344 892 8979
E-Mail Address
niteshdsingh#gmail.com<mailto:niteshdsingh#gmail.com>
Phone Number
123456789
Postcode
421202
City
test
Message
test
Best Regards,

I don't think this regex can be regarded as a generic email parser... rather it will only work for the format that you have provided:
Last\s+Name(?:\n)+((?: *\w+)+)|First\s+Name(?:\n)+((?: *\w+)+)|By phone on:((?: *\d+)+)|(?:E-Mail\s+Address(?:\n)+((?:(?: *\w+)+)#[^\.]+\.[^<]+))|(?:Phone Number(?:\n)+((?: *\w+)+))|(?:Postcode(?:\n)+((?: *\w+)+))|(?:Message(?:\n)+((?: *\w+)+))
Regex 101 Demo
Here in the following groups you get your desired data:
Group 1. Last Name
Group 2. First Name
Group 3. By phone on
Group 4. email
Group 5. Phone Number
Group 6. Postcode
Group 7. Message
UPDATED AS PER THE OP COMMENT:
(?:E-Mail\s+Address(?:\n)+((?:(?: *\w+)+)#[^\.]+\.[^<]+))|(?:Phone Number(?:\n)+((?: *\w+)+))|(?:Postcode(?:\n)+((?: *\w+)+))|(?:Message(?:\n)+((?: *\w+)+))|(?:City(?:\n)+((?: *\w+)+))
Demo Two

Related

RegEx for extracting multiple words in a passage using Tableau

I have a passage and I need to extract a couple of words from it in tableau. The passage is given below:
This looks like a suspicious account. Please look at the details
below. Name: John Mathew Email:john.mathew#abc.com Phone:+1
111-111-1111 Department: abc
For more enquiries contact: ----
Name, email, phone and the department are in the same line separated by blank spaces. I used the below regex and it works well for the department alone:
regexp_extract([CASE DESCRIPTION],'Department : (.+)')
When I apply this one name, I get:
Name: John Mathew Email:john.mathew#abc.com Phone:+1 111-111-1111
Department: abc
instead of just the name. The same happens with email.
How do I solve this problem?
It looks to me like the issue is that your regex just has '(.+)' as its capture group, which basically means "everything" (after the specified string). Since the fields are all on one line, everything after "name" includes the email, phone, and department. (The regex works with department because it's the last thing on the line.)
So, to make it work right, you need to give your regex something other than the end of the line to stop on. To capture just the name, you need to stop before the Email tag, and so on down the list. Something like
Name = regexp_extract([CASE_DESCRIPTION],'Name: (.+) Email:')
email = regexp_extract([CASE_DESCRIPTION],'Email: (.+) Phone:')
phone = regexp_extract([CASE_DESCRIPTION],'Phone: (.+) Department:')
department = regexp_extract([CASE_DESCRIPTION],'Department: (.+)')

How to create Regular Expressions for Israel landlines and mobile phone validation specific phone codes?

Task is:
Create Regular Expressions for Israel landlines and mobile phone validation specific phone codes
Specific phone codes is :
02,03,04,08,09,050,051,052,053,054,055,056,058,059,072,074,076,077,078,079
Validate entered phone does not start with "0" or "1".
Phone number length always 7 for mobile and landline phone numbers.
Already create reg exp for this task:
/^(02|03|04|06|08|09|072|074|076|077|078|079|050|051|052|053|054|055|056|058|059)((?:(?![1,0]{1}))\d{7})$/
You can visit the link to check :
https://regex101.com/r/GRTns5/1/
How to optimize or another solution?
You can simplify a bit with:
^0(?:[234689]|5[0-689]|7[246789])(?![01])(\d{7})$
Demo
The only thing I can optimize here is the first group with phone codes:
^(0(?:5[0-689]|7[246-9]|[2-4689]))((?:(?![1,0]{1}))\d{7})$
Demo

Regex pattern in salesforce apex

I am new to regex.
I have a String formatted like below
Street Name
City, StateCode ZipNumber
for example, the string can be like
50 Connecticut Avenue
Norwalk, CT 06850
or
123 6th Avenue
New York, NY 10013
or
4TH Highway 6
Rule, TX 79547
I am trying to construct a regex here.
But cannot proceed as I have a little idea about regex.
Can you please help me?
The following might be enough :
^(?<Street>[^\n]+)\n(?<City>[^,]+), (?<StateCode>[A-Z]{2}) (?<Zip>\d+)$
It captures the following segments in different groups :
the first line in a group named Street
the part of the second line which precedes the comma in a group named City
the next two capital letters in a group named StateCode
the following digits in a group named Zip

Regex to separate addresses information

I'd like to separate the data input by my user on their shipping information into the respective field.
The data input will be in this format:
Name - Phone Number - Address
For Example:
Andy Leblanc (0123-123-12312) Seaview Av. Street no 21, East Singapore 11221
Name could be any characters, phone would be number with these characters possibly included: "()[]./- ", and address would again be any character.
So the 2 characters field are separated by a number field. Is this possible to separate using regex?
I came up with the following solution.
My search regex is:
([\w\s]+)\s([\(\)\d-\[\]\.\/-]+)\s(.*)
My replacement string is:
Name:\1#Phone:\2#Address\3
Edit: Included more characters in the phone section.

Need help finding a phone number and a string using regular expressions

I am new to regex and I an trying use regex to find any number of characters that comes after the question mark and before the pipe, along with the phone number. Can anyone please help me? Below is a sample URL ?
http://www.myurl.com?demo|15555555555
regular expression should find "demo" and "15555555555"
here is a regex with named capturing groups, also I tried to cover many possibilities for different number formats
Regex
(?<=\?)(?'name'[\w\s%]+)\|(?'phone'[\+\w-\(\)\s%]+)(?:\n|$)
Test string
http://www.myurl.com?demo|15555555555
http://www.myurl.com?Local|754-3010
http://www.myurl.com?Domestic|(541) 754-3010
http://www.myurl.com?International|+1-541-754-3010
http://www.myurl.com?Dialed in the US|1-541-754-3010
http://www.myurl.com?Dialed from Germany|001-541-754-3010
http://www.myurl.com?Dialed from France|191 541 754 3010
http://www.myurl.com?skype|155555-SKYPE
http://www.myurl.com?an%20escaped%20name|191%20541%20754%203010
Result
MATCH 1
name [21-25] demo
phone [26-37] 15555555555
MATCH 2
name [59-64] Local
phone [65-73] 754-3010
MATCH 3
name [95-103] Domestic
phone [104-118] (541) 754-3010
MATCH 4
name [140-153] International
phone [154-169] +1-541-754-3010
MATCH 5
name [191-207] Dialed in the US
phone [208-222] 1-541-754-3010
MATCH 6
name [244-263] Dialed from Germany
phone [264-280] 001-541-754-3010
MATCH 7
name [302-320] Dialed from France
phone [321-337] 191 541 754 3010
MATCH 8
name [359-364] skype
phone [365-377] 155555-SKYPE
MATCH 9
name [444-463] an%20escaped%20name
phone [464-486] 191%20541%20754%203010
try demo here
if you create that link yourself, I would recommed you to write a common $GET-variable. you won't need any regex.
http://www.myurl.com?demo=15555555555
echo htmlspecialchars($_GET["demo"]);
You can use this regex:
\?(\w+)\|(\d+)
Working demo
MATCH 1
1. [21-25] `demo`
2. [26-37] `15555555555`