Help with Reged

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
rlobianco
Forum Newbie
Posts: 4
Joined: Sat Jul 24, 2010 2:34 pm

Help with Reged

Post by rlobianco »

Hi,I'm trying to pull a purchase order number from the following string..

"Reseller PO - 23213123-123-232331"

All I need is the number. I tried (Reseller PO\s*-.\A*) it works with one online regex tester but not some others so I must have something wrong..

Help... :)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Help with Reged

Post by AbraCadaver »

Dunno why you're using \A, but this should work if the strings are consistent:

[text]/Reseller PO - ([-\d]+)/[/text]
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
rlobianco
Forum Newbie
Posts: 4
Joined: Sat Jul 24, 2010 2:34 pm

Re: Help with Reged

Post by rlobianco »

That syntax seems to pull the entire string..I need to just pull the number after the dash. The number can vary greatly though, Some are in the format I posted and some are just number with no dashes. I thought the \A would start the capture at the number.

Regards,
Ralph
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Help with Reged

Post by AbraCadaver »

rlobianco wrote:That syntax seems to pull the entire string..I need to just pull the number after the dash. The number can vary greatly though, Some are in the format I posted and some are just number with no dashes. I thought the \A would start the capture at the number.

Regards,
Ralph
It depends upon what language you're using and how it returns matches. You should be able to pull the match for the first capture group (). In PHP it would be $matches[1] for this:

Code: Select all

preg_match('/Reseller PO - ([-\d]+)/', $search_string, $matches);
[text]Array
(
[0] => Reseller PO - 23213123-123-232331
[1] => 23213123-123-232331
)[/text]
And an array in $matches[1] for this:

Code: Select all

preg_match_all('/Reseller PO - ([-\d]+)/', $search_string, $matches);

Code: Select all

Array
(
    [0] => Array
        (
            [0] => Reseller PO - 23213123-123-232331
        )
    [1] => Array
        (
            [0] => 23213123-123-232331
        )
)
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply