Jump to content

regex help


doug007

Recommended Posts

Hi Fellas,

 

Thanks to effigy the Great who devised the belwo regex on my code i was able to match the data below.  i am now trying to match one more pieces of data:

 

Current Data:

<PostCode>W1T 5AE</PostCode>

<PostCode>W1T 5A E </PostCode>

<PostCode>W1T 5AE-</PostCode>

 

Current regex:

 

preg_match_all('%

  (?<=<PostCode>)

  (

      ### 9 characters or more.

      [^<]{9,}

      |

  ### A hyphen.

      [^<]*?-[^<]*?

      |

  ### More than 1 space.

      [^<]*?\s[^<]*?\s[^<]*?

     

  )

  (?=</PostCode>)%x',

 

the above works fine.

 

Now I also want to match this data:

 

<PostCode>1T 5AE</PostCode>

<PostCode>18T 5AE</PostCode>

 

i tried the following:

 

### A number

[^<]\d*?[^<]*?

 

but returns all postcodes???  it would be great if someone could care to help?

 

function parser()
{
    $file_path= file_get_contents('xml.txt');
    $file_count = count($file_path); 
    
    	if($file_count==0)
   	 {
      	print('file empty');
  	 }
   
       
       preg_match_all('%
  						 (?<=<PostCode>)
  							 (
     							  ### 9 characters or more.
						      [^<]{9,}
						      |
						      ### A hyphen.
						      [^<]*?-[^<]*?
						      |
							  ### A number.
						      [^<]\d*?[^<]*?
						      |
						      ### More than 1 space.
						      [^<]*?\s[^<]*?\s[^<]*?
						      
   							)
   						(?=</PostCode>)%x', 
       							
       $file_path, $matches);

	if ($matches) 
		{
			$counter = count($matches[0]);
			echo'<h1><font color=red>Invalid PostCodes found :'.$counter.'</font><br />';

				while(list($key, $value)=each($matches[0]))
					{
						echo '=><input type=text size=13 value="'.$value.'"><br />';
					}

		} 
}
  
parser();

 

Link to comment
Share on other sites

[^<]*?\d[^<]*?

 

Hi effigy

 

i tried that, but it only matches the end of the string (see below)?  its wiered as the metacharacter (^) should match the start?

 

it matches this data:

 

<PostCode>1T 5AE548</PostCode>

<PostCode>18T 5AE14</PostCode>

 

but should match this:

 

 

<PostCode>1T 5AE</PostCode>

<PostCode>18T 5AE</PostCode>

 

cheers

 

dug

Link to comment
Share on other sites

<pre>
<?php

$data = <<<DATA
	it matches this data:
	<PostCode>1T 5AE548</PostCode>
	<PostCode>18T 5AE14</PostCode>
	but should match this:
	<PostCode>1T 5AE</PostCode>
	<PostCode>18T 5AE</PostCode>
DATA;
preg_match_all('%
	(?<=<PostCode>)
	(
		### 9 characters or more.
		[^<]{9,}
		|
		### A hyphen or digit.
		[^<]*?[-\d][^<]*?
		|
		### More than 1 space.
		[^<]*?\s[^<]*?\s[^<]*?
	)
	(?=</PostCode>)
%x', $data, $matches);
print_r($matches);
?>
</pre>

Link to comment
Share on other sites

<pre>
<?php

$data = <<<DATA
	it matches this data:
	<PostCode>1T 5AE548</PostCode>
	<PostCode>18T 5AE14</PostCode>
	but should match this:
	<PostCode>1T 5AE</PostCode>
	<PostCode>18T 5AE</PostCode>
DATA;
preg_match_all('%
	(?<=<PostCode>)
	(
		### 9 characters or more.
		[^<]{9,}
		|
		### A hyphen or digit.
		[^<]*?[-\d][^<]*?
		|
		### More than 1 space.
		[^<]*?\s[^<]*?\s[^<]*?
	)
	(?=</PostCode>)
%x', $data, $matches);
print_r($matches);
?>
</pre>

 

what took you soo long :)    works perfect :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.