CoreCoder
CoreCoder

Reputation: 419

Regular expression for alphanumeric characters

need regular expression which could match a string like that "1B7FL26X3WS731388". Alphanumeric 17 character lenght.

I am using this expression.

$rEX    =  '/([A-Z0-9]){17}/';

but it also return a portion from a string like this "FGD798791B7FL26X3WS731388POPOD";

I need to select a string which is exactly 17 character long 18th character should not be Alphanumeric.

Upvotes: 0

Views: 368

Answers (6)

shift66
shift66

Reputation: 11958

$rEX = '/[^A-Z0-9]+([A-Z0-9]){17}[^A-Z0-9]+/';

Upvotes: 0

Adeel Mughal
Adeel Mughal

Reputation: 736

try this....

<?php
$title='1B7FL26X3WS731388';
$result = preg_replace("/[^a-zA-Z0-9]/", "", $title);
echo strlen($result);
?>

Upvotes: -1

Abraham Covelo
Abraham Covelo

Reputation: 961

You should use ^ $ delimiters

$rEX = '/^([A-Z0-9]){17}$/';

To only allow uppercase alphanumeric 17 length string

You regular expression will allow all strings that contains a SUBSTRING of uppercased alphanumeric 17 lenght string.

Upvotes: 1

Jayy
Jayy

Reputation: 2436

This will do

^([a-zA-Z0-9]){17}([^a-zA-Z0-9])*$

Upvotes: 0

Basti
Basti

Reputation: 4042

Add begin and end to your pattern:

$rEX = '/^([A-Z0-9]){17}$/D';

Upvotes: 2

Chowlett
Chowlett

Reputation: 46675

$rEX = '/[^A-Z0-9]([A-Z0-9]){17}[^A-Z0-9]/'; should do. [^...] negates the character class.

Upvotes: 0

Related Questions