Solved

help amend this regex to only include part numbers that have both letters and numbers

Posted on 2012-03-20
10
334 Views
Last Modified: 2012-07-27
I have the following regex to parse a string looking for likely candidates for part numbers/serial numbers.
$regexForModelNo = "@\b((?=[A-Za-z/ -]{0,13}\d)[A-Za-z0-9/ -]{3,14})\b@";

Open in new window

It works fine but the odd time it spits out some random matches along with good matches, for example it sometimes includes parts of words that arent really any likely match to part numbers. How would I modify the above to check that all matched parts must contain at least one number in there?
0
Comment
Question by:Slimshaneey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37741932
It would help to know the input values, and the values that don't match correctly...
0
 
LVL 11

Author Comment

by:Slimshaneey
ID: 37741942
An example would be "Stringstringstring  part rb212x stringstringstring" would return "part rb212x", but I want to ignore the "part bit as it contains no numbers.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37741965
Thank you.  I just realized that it would help to know where you are using this regular expression (PHP, .NET, Perl, ...)?
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 11

Author Comment

by:Slimshaneey
ID: 37742163
Its PHP, sorry,  should have mentioned in opening post
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37742174
Can you provide the rules for a valid serial number? You've got spaces in your regex, but I would think that a serial number wouldn't contain spaces.
0
 
LVL 13

Assisted Solution

by:Carl Bohman
Carl Bohman earned 250 total points
ID: 37742591
Based on what you've said so far, perhaps just removing the space from the look-ahead assertion would give you the results you're looking for:
$regexForModelNo = "@\b((?=[A-Za-z/-]{0,13}\d)[A-Za-z0-9/ -]{3,14})\b@";

Open in new window

With that change, the first part of the serial number must contain a number, but can also contain leters or a dash.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 37744513
You might be asking too much of a single regular expression.  A function like this may be easier to write and debug.
http://www.laprbass.com/RAY_temp_slimshaneey.php

You might also enjoy reading about test-driven development.  The article looks like it is about building regular expressions, but it is really about how to think about programming problems in a practical and structured way.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html
<?php // RAY_temp_slimshaneey.php
error_reporting(E_ALL);
echo "<pre>";

// THE TEST DATA
$txt = "Stringstringstring  part rb212x stringstringstring or A212x";

// A FUNCTION TO FIND SERIAL NUMBERS
function find_sn($str)
{
    // A REGULAR EXPRESSION TO ISOLATE SUBSTRINGS
    $rgx
    = '#'             // REGEX DELIMITER
    . '\b'            // WORD BOUNDARY
    . '('             // GROUP
    . '.*?'           // OF ANYTHING
    . ')'             // END GROUP
    . '\b'            // WORD BOUNDARY
    . '#'             // END REGEX
    ;

    // A REGULAR EXPRESSION TO MATCH LETTERS
    $ltr = '#[A-Z]#i';

    // A REGULAR EXPRESSION TO MATCH NUMBERS
    $num = '#[0-9]#';

    if (!preg_match_all($rgx, $str, $mat)) return FALSE;

    $out = array();

    // LOOK FOR BOTH LETTERS AND NUMBERS IN EACH OF THE SUBSTRINGS
    foreach ($mat[0] as $txt)
    {
        if (preg_match($ltr, $txt))
        {
            if (preg_match($num, $txt))
            {
                $out[] = $txt;
            }
        }
    }

    // ANY FINDINGS?
    if (empty($out)) return FALSE;
    return $out;
}

// TEST THE FUNCTION
print_r( find_sn($txt) );

Open in new window

0
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 37744980
If you can define what is being looked for, it is a lot easier to create a regex (or any function) to find it.  TheLearnedOne asked about positive and negative test cases.  kaufmed asked for the definition of what you are looking for.  Either of these would help in coming up with the proper regex (or function, if you want).

Based on what has been written so far, it sounds like a serial number is defined as a sequence of characters with the following attributes:
1. 3-14 characters in length;
2. Contains a combination of letters, numbers, dashes, slashes (/), and spaces;
3. Is not touching other words (i.e., contained between word boundaries);
4. Contains at least one number; and
5. (I assume) Contains at least one number prior to any spaces.

Adding more description to the definition (or correcting errors in it) will allow for more refining of the regex.
0
 
LVL 11

Author Closing Comment

by:Slimshaneey
ID: 38231316
Thanks guys, I looked at many options on this, it ended up being a much more simple regex query (Ive included it below, it basically finds groups of clustered characters that contains at least a number). You advice and insight set me on a very productive learning curve though. Sorry for getting back to this so late. Finally getting round to some cleanup.

[A-Za-z0-9-.]{4,}(?<=\d)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question