473,394 Members | 1,554 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,394 software developers and data experts.

Specific Regular Expression (PREG) Help

Hey all,

My first foray in to regular expressions has not been easy, with the Perl-style regular expressions that preg_match requires complicating things slightly for me, as I find that many of the email or website seeking regex I find simply won't work... Probably for some small reason attributable to the different style, that I'm unable to discern.

The code that I'm trying to write is looking for a specific email address, website, and also a name within html source, which looks like the following:

<br> E-mail: <a href="mailto:email@address.com">email@address.com</a><br>
<br> Web site: <a href="http://www.address.com">www.address.com</a><br>

<td>
Vice President <br /> Joe Smith <br>
</td>

Wanting to collect the e-mail, url, and name, can someone help with conjuring up a regular expression I can plug in to preg_match?

Thanks very much
Jun 19 '10 #1

✓ answered by dlite922

regex for email and URL you can easily find on the web, however for the name it's going to be a little more difficult to find.

If every html source you're going to look through is that exact format (i.e. it will always say Vice President space <br/> Name name </br>

Then yes, you can write a regexp for that. Just that literal string is enough:

Expand|Select|Wrap|Line Numbers
  1. preg_match('/Vice President <br />.+<br>/',$line)
  2.  
With regex there's no one correct answer, the answer depends on your use and _exactly_ what you want. You might think you were pretty thorough in your post, but you can get much more detailed than that.

For example you can write a regex to compare the mailto address to make sure it's exactly the same as content of the link. I.e you can make it not match the following:

<a href="mailto:someaddress@domain.foo">somethingelse </a>

Cheers,


Dan

3 1513
dlite922
1,584 Expert 1GB
regex for email and URL you can easily find on the web, however for the name it's going to be a little more difficult to find.

If every html source you're going to look through is that exact format (i.e. it will always say Vice President space <br/> Name name </br>

Then yes, you can write a regexp for that. Just that literal string is enough:

Expand|Select|Wrap|Line Numbers
  1. preg_match('/Vice President <br />.+<br>/',$line)
  2.  
With regex there's no one correct answer, the answer depends on your use and _exactly_ what you want. You might think you were pretty thorough in your post, but you can get much more detailed than that.

For example you can write a regex to compare the mailto address to make sure it's exactly the same as content of the link. I.e you can make it not match the following:

<a href="mailto:someaddress@domain.foo">somethingelse </a>

Cheers,


Dan
Jun 22 '10 #2
@dlite922
Thanks very much for your helpful reply Dan.

I had a rough idea of how the regex would work, but the regex you post is extremely helpful as I was trying to find the correct syntax for 'capture everything between,' which is part of your regular expression. Just one syntactical issue with your preg... It seems to grab both the name and the html tags also... Is it possible to only get the name?

Thanks very much!
Jun 29 '10 #3
@dlite922
I just figured it out! By using (.+), I can use the handy feature of preg_match, that successive indices after the zeroth one (the full match) reference the parenthesized matches.
Jun 30 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: hq4ever (at) 012 (dot) net (dot) il | last post by:
function testemail($email) { $validEmailExpr = "^(?)*@(?)*$"; return eregi($validEmailExpr, $email); } $email = "foo@bar.gov.mil"; testmail($email); //return TRUE
1
by: Martin Andert | last post by:
Hello, i want to parse some html with regex and have the following problem: --- html to parse start --- some text <span class="x"> some text with linebreaks and tabs and <b>tags <i>in...
1
by: adidev | last post by:
hello, anyone know how to return any quotes used inside a text node? for example: <node1>This is my text node, my quote "quote" is here</node1> what i want to do is escape those " with...
5
by: Mahesha | last post by:
Hello, I need help in replacing one string pattern with another. Ex: I have a financial security expression like log(T 3.25 6/24/2004)/sqrt(T 4.5 6/19/2002) Here "T 3.25 6/24/2004" is a...
3
by: | last post by:
Hi I need to write one regex to read all the fields from the following lines / file format line 1 - some_alphanumeric,some_alphanumeric,"something, something",numbers_hyphenatedORnot line 2 -...
3
by: Joe | last post by:
Hi, I have been using a regular expression that I don’t uite understand to filter the valid email address. My regular expression is as follows: <asp:RegularExpressionValidator...
3
by: Craig | last post by:
Hi I'm having some troubles getting my regex to work. I have a string as follows The "quick and brown" fox "jumped over the" lazy dog. The output should be as follows: The "quick and brown"...
4
by: Ernesto | last post by:
I'm trying to get the right syntax for my regular expression. The string I'm trying to parse is: # myString Name: David Dude Right now, I'm using the following code:
1
by: BHPexpert | last post by:
Regular Expression help needed -------------------------------------------------------------------------------- I want to extract all text that is contained inside the brackets after the word...
1
by: Matthias S. | last post by:
Hi there, I'm trying to build a regular expression which will do a replace for me. I'm getting grey hair on this one: here is my input string: <a href="http://test.com/fun.jpg"...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.