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
https://forums.phpfreaks.com/topic/91242-regex-help/
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
https://forums.phpfreaks.com/topic/91242-regex-help/#findComment-469424
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
https://forums.phpfreaks.com/topic/91242-regex-help/#findComment-469633
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
https://forums.phpfreaks.com/topic/91242-regex-help/#findComment-469763
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.