473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Parse a string with an email address

I need to parse a string with an embedded email address.
The string always has the format NAME (name@domain) SOMETEXT.
What I need to get is the email address as name@domain.

I came up with this (I know it's broken, but it's a first start):

<?php

function ParseTicketEmail($ticket)
{
global $TicketEmail;
for ($i=0; $i< strlen($ticket); $i++){
//we need to pass everything between ( and ) to $TicketEmail nothing else
do {
$ticket[$i] = $discarded;
} while ($ticket[$i] != '(');
while ($ticket[$i] != ')'){
$TicketEmail = $TicketEmail . $ticket[$i];
}
}
return $TicketEmail;
}

$ticket = "hello (wo***@world.com) bla";
print "$TicketEmail";
print "$discarded";
?>

But that doesn't return anything for either $TicketEmail or $discarded.
So
a: why doesn't it return anything for either string? (It's too early in the morning here)
b: does anyone have such a function anywhere for me to check out for how to do it?

/M.
--
Martin Skjöldebrand
Family site: http://www.skjoldebrand.org
"Art" site: http://martoni.deviantart.com
Public key available at: http://wwwkeys.pgp.net
Jul 17 '05 #1
9 4527
Martoni wrote:
I need to parse a string with an embedded email address.
The string always has the format NAME (name@domain) SOMETEXT.
What I need to get is the email address as name@domain.

I came up with this (I know it's broken, but it's a first start):

<?php

function ParseTicketEmail($ticket)
{
global $TicketEmail;
for ($i=0; $i< strlen($ticket); $i++){
//we need to pass everything between ( and ) to $TicketEmail nothing else
do {
$ticket[$i] = $discarded;
} while ($ticket[$i] != '(');
while ($ticket[$i] != ')'){
$TicketEmail = $TicketEmail . $ticket[$i];
}
}
return $TicketEmail;
}

$ticket = "hello (wo***@world.com) bla";
print "$TicketEmail";
print "$discarded";
?>

But that doesn't return anything for either $TicketEmail or $discarded.
So
a: why doesn't it return anything for either string? (It's too early in the morning here)
b: does anyone have such a function anywhere for me to check out for how to do it?


Why are you trying to use a self-made string parsing routine for such
jobs when _regular expression pattern matching_ can do it for you in one
single call ?

Here is a sample that should work:
preg_match( '/(\(.*\?.*\))/', $ticket, $matches );

For later information about *preg_match* and *regular expression
expression* see followings:
<http://fr.php.net/manual/en/function.preg-match.php>
<http://fr.php.net/manual/en/ref.pcre.php>

--
Guillaume Brocker
Jul 17 '05 #2
Martoni wrote:
b: does anyone have such a function anywhere for me to check out for how to do it?


Why are you trying to use a self-made string parsing routine for such
jobs when _regular expression pattern matching_ can do it for you in one
single call ?

Here is a sample that should work:
preg_match( '/(\(.*@.*\))/', $ticket, $matches );

For later information about *preg_match* and *regular expressions*, see
followings:
<http://fr.php.net/manual/en/function.preg-match.php>
<http://fr.php.net/manual/en/ref.pcre.php>

--
Guillaume Brocker
Jul 17 '05 #3
Guillaume Brocker <gu***************@ircad.u-strasbg.fr> writes:
Martoni wrote:
b: does anyone have such a function anywhere for me to check out for how to do it?
Why are you trying to use a self-made string parsing routine for such
jobs when _regular expression pattern matching_ can do it for you in
one single call ?


Of course that is the way.
Why I didn't use reg exp? I get that allergical perl itch...
(I hate reg exps).
Here is a sample that should work:
preg_match( '/(\(.*@.*\))/', $ticket, $matches );
I'll give it a go ...

For later information about *preg_match* and *regular expressions*,
see followings:
<http://fr.php.net/manual/en/function.preg-match.php>
<http://fr.php.net/manual/en/ref.pcre.php>


And I'll have a look at it ...

/M.

--
Martin Skjöldebrand
Family site: http://www.skjoldebrand.org
"Art" site: http://martoni.deviantart.com
Public key available at: http://wwwkeys.pgp.net
Jul 17 '05 #4
Guillaume Brocker wrote:
Martoni wrote:
I need to parse a string with an embedded email address.
The string always has the format NAME (name@domain) SOMETEXT.
What I need to get is the email address as name@domain.


preg_match( '/(\(.*\?.*\))/', $ticket, $matches );


Guillaume Brocker wrote in
<news:40**********************@news.free.fr>:

| preg_match( '/(\(.*@.*\))/', $ticket, $matches );

There's no need to use a caturing subpattern; $matches[0] contains the
full pattern match.

A right parenthesis after the parenthesised email address would muck
up the matched address. Greedy matching will gobble everything till
the last right parenthesis in the string. Simply inverting the
greediness doesn't fix anything. Consider my address:

john+usenet@(Jock's domain)johndunlop.info

Of course, if parentheses aren't allowed before or after the
parenthesised email address, your pattern above is almost there.

In trying to match email addresses with regular expressions alone,
there are two certainties: (1) failure, and (2) an onslaught of
trichotillomania.

--
Jock
Jul 17 '05 #5
jo*********@johndunlop.info (John Dunlop) writes:
Guillaume Brocker wrote:
Martoni wrote:
> I need to parse a string with an embedded email address.
> The string always has the format NAME (name@domain) SOMETEXT.
> What I need to get is the email address as name@domain.


preg_match( '/(\(.*\?.*\))/', $ticket, $matches );


Guillaume Brocker wrote in
<news:40**********************@news.free.fr>:

| preg_match( '/(\(.*@.*\))/', $ticket, $matches );

There's no need to use a caturing subpattern; $matches[0] contains the
full pattern match.

A right parenthesis after the parenthesised email address would muck
up the matched address. Greedy matching will gobble everything till
the last right parenthesis in the string. Simply inverting the
greediness doesn't fix anything. Consider my address:

john+usenet@(Jock's domain)johndunlop.info

Of course, if parentheses aren't allowed before or after the
parenthesised email address, your pattern above is almost there.


Not having experience with this, do you mean that something like:
Name (email) Some text (more text) even more text

would make the preg_match to fail? One problem I see is that the paranthesis
are kept, not dropped.
/M.
--
Martin Skjöldebrand
Family site: http://www.skjoldebrand.org
"Art" site: http://martoni.deviantart.com
Public key available at: http://wwwkeys.pgp.net
Jul 17 '05 #6
Martoni wrote:
jo*********@johndunlop.info (John Dunlop) writes:
Guillaume Brocker wrote in
<news:40**********************@news.free.fr>:

| preg_match( '/(\(.*@.*\))/', $ticket, $matches );

Of course, if parentheses aren't allowed before or after the
parenthesised email address, your pattern above is almost there.
Not having experience with this, do you mean that something like:
Name (email) Some text (more text) even more text

would make the preg_match to fail?


Yes. The pattern is greedy, so matches from the first left
parenthesis to the last right parenthesis in the subject string.
Consider:

preg_match(
'`\(.*@.*\)`',
'Mr. F. Bar (fo*@bar.example) baz (bum) bee.',
$matches)

The content of $matches[0] is:

(fo*@bar.example) baz (bum)
One problem I see is that the paranthesis are kept, not dropped.


Aye, that's the problem. If parentheses are only used to demarcate
the address, the problem doesn't exist. Simply capture everything
from the first left parenthesis to the last right parenthesis:

preg_match(
'`\(.*\)`s',
$subject,
$matches)

--
Jock
Jul 17 '05 #7
John Dunlop <jo*********@johndunlop.info> writes:
preg_match(
'`\(.*@.*\)`',
'Mr. F. Bar (fo*@bar.example) baz (bum) bee.',
$matches)

The content of $matches[0] is:

(fo*@bar.example) baz (bum)


Hmm,
Not good at all.
I've never seen paranthesis used in our trouble tickets but if someone decides to enter his/her new novella as a trouble ticket then end it all with (just like Charlie experienced last week) then it will be a very nasty bug indeed.

Ofcourse one could boldly truncate the ticket string at an educated guess to include the "header". Or coming to think of it couldn't one read the string to get the character sequence number of the first ")" then truncate the string at "that number"+1 then do the preg_match.

/M.

--
Martin Skjöldebrand
Family site: http://www.skjoldebrand.org
"Art" site: http://martoni.deviantart.com
Public key available at: http://wwwkeys.pgp.net
Jul 17 '05 #8
Martoni wrote:
Or coming to think of it couldn't one read the string to get the
character sequence number of the first ")" then truncate the string
at "that number"+1 then do the preg_match.


Non-greedy matching would suffice if it were that simple. But it's
not. A right parenthesis can appear as part of an address.

Why does a single string contain the name, address and other data?

--
Jock
Jul 17 '05 #9
John Dunlop <jo*********@johndunlop.info> writes:
Martoni wrote:
Or coming to think of it couldn't one read the string to get the
character sequence number of the first ")" then truncate the string
at "that number"+1 then do the preg_match.


Non-greedy matching would suffice if it were that simple. But it's
not. A right parenthesis can appear as part of an address.

Why does a single string contain the name, address and other data?


For historical reasons the ticket is formated that way.
I will "soon" start rewriting the whole thing, but for maintenance and adding
some stuff, mostly minor features, I have work around the current faulty design.
The ticket includes name and email of the person submitting it, and I thought
it would be neat in some cases to extract that info.

/M.

--
Martin Skjöldebrand
Family site: http://www.skjoldebrand.org
"Art" site: http://martoni.deviantart.com
Public key available at: http://wwwkeys.pgp.net
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jesse | last post by:
I have a string with the following information. $myString = "This is my example string please. Please visit http://www.mylink.com . More example text goes here. another link http://www.link2.com...
6
by: Dave Kuhlman | last post by:
Suppose that I have content that looks like what I've included at the end of this message. Is there something in the standard Python library that will help me parse it, break into the parts...
4
by: Bill | last post by:
If, for example, I retrieve a connectionstring from a config file using something like: Value = ConfigurationSettings.AppSettings; This will return a string that is semi-colon delimited. If I...
0
by: rdi | last post by:
I'm attempting to use regular expressions to parse email messages (I'm already able to download the message--I just need to parse them). I need to extract the "From", "To", "BCC" (if any) &...
9
by: Python.LeoJay | last post by:
Dear all, i need to parse billions of numbers from a file into float numbers for further calculation. i'm not satisfied with the speed of atof() function on my machine(i'm using visual c++ 6)....
5
by: meendar | last post by:
Hi, I just want to parse a character string as below Char * c=abcsyd"loddggg"kjskjdfsdf; I need to stripe out loddgg from c (inside ""). How can i do this in c?
11
by: Peter Afonin | last post by:
Hello, I need to parse a string that returns the domain DNS records and to put this data into a DataTable. I don't have much experience in parsing strings, so I'm not aware of the efficient way...
7
by: Cirene | last post by:
I have a long string (MyString) with several token/value combinations. Here's an example... NAME: JOHN ADDRESS: 123 MAIN ST COUNTY: SOMEWHERE PHONE: 333-222-5151 x542 ...
3
by: broll911 | last post by:
I am gettin a error message on this script can someone help me. I am a complete newbie on this stuff. Thanks in advance for any help you can give me. here is the error. Parse error: syntax error,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.