Jump to content

How to Match a Part Number


imperium2335

Recommended Posts

<?php 

$expr = '/(?:^| )((?:[0-9]+[a-z]|[a-z]+[0-9])[a-z0-9]+)/si';
$str = 'hahaha ae232a4d zomg wut wut wut asgfe 3231aa3dw';

preg_match_all( $expr, $str , $matches, PREG_PATTERN_ORDER );

header( 'Content-type: text/plain' ); // display in plain text
print_r( $matches[1] );

?>

 

returns

 

Array
(
    [0] => ae232asd
    [1] => 3231aadw
)

 

It's slightly faster than positive look-ahead.

 

It first searches for a space, or the start the string/new line

It then checks for either letters followed by a number, or numbers followed by a letter

It then captures the remaining letters or numbers

What does the 's' do in the /si part?

Modificator 's' - ignoring chars of new line "\r\n" or "\n".

Modificator 'i' - ignoring the case of letters.

JavaScript haven't the modifocator 's', instead of this use modif. 'g'

Try this

var partTest = /(?:^| )((?:\d+[a-z]|[a-z]+\d)[a-z\d]+)/gi;

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.