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?
 
b0lsc0ttConnect With a Mentor IT ManagerCommented:
The easiest way to do it is ...

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

Use single quotes instead.

bol
0
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
All Courses

From novice to tech pro — start learning today.