tags:

views:

40

answers:

2

I need to match a substring in php substring is like

<table class="tdicerik" id="dgVeriler"

I wrote a regular expression to it like <table\s*\sid=\"dgVeriler\" but it didnot work where is my problem ?

A: 

I dont know what you want get but try this:

<table\s*.*id=\"dgVeriler\"
Sebastian Brózda
Doesn't work if there is, somewhere after, the string `id="dgVeriler"`. You should make your regex ungreedy by adding `?` after the `.*` like `.*?`.
M42
sure, after adding something else, but for <table class="tdicerik" id="dgVeriler", should be ok. Author dont mention what he want :) only say "match", so.. this regex match to his string.
Sebastian Brózda
+1  A: 

You forgot a dot:

<table\s.*\sid="dgVeriler"

would have worked.

<table\s+.*?\s+id="dgVeriler"

would have been better (making the repetition lazy, matching as little as possible).

<table\s+[^>]*?\s+id="dgVeriler"

would have been better still (making sure that we don't accidentally match outside of the <table>tag).

And not trying to parse HTML with regular expressions, using a parser instead, would probably have been best.

Tim Pietzcker
A simple DOM `getElementById()` or perhaps an XPath `//table[@id='dgVeriler']` would be preferable indeed.
Wrikken
thanks it works
Shahriar