Regular Expression to Prefix Search Results with "000"

Posted on 2013-10-03
Medium Priority
Last Modified: 2013-11-14
I want to prefix some data using a reqular expression.  I am using this expression to locate all UPC numbers that are 11 digitis in length.  ^\d{11}$   but I want to know if I can prefix the results with "000".  

Locate this:                              Transform to this:

20349098761                           00020349098761
Question by:hojohappy
  • 2
  • 2
LVL 35

Expert Comment

by:Terry Woods
ID: 39545006
What language or tool are you using?

If it was php you'd use something like:
$data = preg_replace('/^(\d{11})$/','000$1',$data);
LVL 35

Accepted Solution

Terry Woods earned 2000 total points
ID: 39545048
Using a lookahead is more elegant and should also work:
$data = preg_replace('/^(?=\d{11}$)/','000',$data);

Expert Comment

by:Derek Jensen
ID: 39600849
preg_replace_all can prefix all of them at once:

$data = preg_replace_all('/^(?=\d{11}$)/','000',$data);  

Open in new window

LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39607413

Which documentation are you looking at? preg_replace_all doesn't appear to be a valid PHP function, and the preg_replace that Terry mentions will replace every occurrence by default--unless you specify how many occurrences to replace by passing an additional parameter.

Expert Comment

by:Derek Jensen
ID: 39609274

You're absolutely right, I'm a tardbucket...I was thinking of preg_match_all. My bad. :">

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

587 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