Regular expression help

Hello I am so new to regular expressions I have this simple expression
Below in the Snippet.  
"/content=\"(.*)\/>/" seems to work but the 2nd part  /> is really   "/>  

When I try to escape the "    It doesn't return any data. here is what I did \"\/>

I am not sure exactly what needs to be escaped and what doesn't??? 0.o

Do you always just use   -->  \   for escaping ?
$do6 = preg_match_all("/content=\"(.*)\/>/", $test, $matches6);

Open in new window

LVL 3
jbrashear72Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

b0lsc0ttIT ManagerCommented:
The easiest way to do it is ...

$do6 = preg_match_all('/content="(.*)"\/>/', $test, $matches6);

Use single quotes instead.

bol
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jbrashear72Author Commented:
hmm that doesn't seem to work.
I don't know why it seems to be so hard.. (=


-J

///////////////////////////////////////////////////////////////////////
// $do6 = preg_match_all("/content=\"(.*)\/>/", $test, $matches6);
$do6 = preg_match_all('/content="(.*)"\/>/', $test, $matches6);
 
foreach ($matches6 as $val) {
//    echo "matched: " . $val[0] . "\n";
 
$desc = $val[0];
}
/////////////////////////////////////////////////////////////////////
 <? echo $desc; //  returns nothing with single ticks ?>  

Open in new window

0
jbrashear72Author Commented:
Wait it did work.

there was a white space:
" />

I thought it was:
"/>



Is there a way to not care about white space?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

jbrashear72Author Commented:
So how would I know in this string what needs to be escaped?

<div id="tabSpecification" style="display:none;width:100%;">
0
jbrashear72Author Commented:
or look at this I have the code below

I want to look at this html page and get
http://www.mydomain.com/imagename.jpg

What is the best way to grab this using regular expressions?
 <td  align="left" valign="top">
        
        <img src="http://www.mydomain.com/imagename.jpg" alt="imagenametxt" style="margin-bottom: 10px; margin-right:10px;" />
                        
        
        <br />

Open in new window

0
b0lsc0ttIT ManagerCommented:
If you don't want it to require the whitespace the use a question mark after it.  For example ...

$do6 = preg_match_all('/content="(.*)" ?\/>/', $test, $matches6);

If both the whitespace and slash should be optional then you could do ...

$do6 = preg_match_all('/content="(.*)"( \/)?>/', $test, $matches6);

Let me know if you have a question or need more info.

bol
0
jbrashear72Author Commented:
So adding the ? says that anything before is optional?
0
b0lsc0ttIT ManagerCommented:
>> So how would I know in this string what needs to be escaped? <<

That is a bit of a loaded question.  Escaping depends on what is being done and what is processing.  For example in the expression here you had to escape for PHP.  In some cases you may also have to escape for the expression itself if you wish to literally use a character with special meaning.

>> What is the best way to grab this using regular expressions? <<

Based just on the text above I would use the expression ...

src="([^"]+)"

... and get the first captured group.  This may of course not work if the text is different.

bol
0
b0lsc0ttIT ManagerCommented:
>> What is the best way to grab this using regular expressions? <<

Yes.  The ? character is a special quantifier that means 0 or 1 times.  It is usually greedy.

bol
0
jbrashear72Author Commented:
>> What is the best way to grab this using regular expressions? <<

Based just on the text above I would use the expression ...

src="([^"]+)"
---------------------------------------------------
So that example showed a part of the HTML in reality its about 1500+ lines of HTML with
many images. I was trying to single that one out based on the uniqueness of the code before it.

so that being said. does src="([^"]+)" work?
0
b0lsc0ttIT ManagerCommented:
I doubt it.  That would only get that specific URL if no other tags used src="FILENAME".  Knowing the text to be searched is the key to making a regular expression. :)

That tag has an alt property that may be unique or may be near or in tags that would help you find it from all of the other tags using src.  If you have a question about this and need help actually making an expression for this situation then open a new question.  Feel free to post the URL of that question here if you would like me to look at it.  Don't forget to provide details on the text to be searched (or even the text) so you get an expression that will work.

Let me know if you have a question about this.

bol
0
jbrashear72Author Commented:
OK lets do that.. Ill past the entire page that I am pulling.. We have a huge static website that I need to try to convert to a database driven site. So I need to pull elements off the current site and add them to a database.
Since all of our pages are the same I thought that it would be a Job for regex.

Ill create the new one and link to it from here.
-J
0
jbrashear72Author Commented:
0
b0lsc0ttIT ManagerCommented:
I'm glad I could help.  I'll  take a look at the new question.  Thanks for the grade, the points and the fun question.

bol
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.