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

Link to comment
Share on other sites

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;

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.