1

I have a pretty large database with some data listed in this format, mixed up with another bunch of words in the keywords column.

BA 093, RJ 342, ES 324, etc.

The characters themselves always vary but the structure remains the same. I would like to change all of the strings that obey this character structure : 2 characters A-Z, space, 3 characters 0-9 to the following:

BA-093, RJ-342, ES-324, etc.

Be mindful that these strings are mixed up with a bunch of other strings, so I need to isolate them before replacing the empty space. Here is a sample string:

Km 111 aracoiaba Araçoiaba sp 270 spvias vias sao paulo Araçoiaba Bidirecional

sp 270 is the bit we want to change.

EDIT: There was also an exception which should ignore the condition in case KM are the first two characters, it was handled by one of the answers

I have written the beginning of the script that picks up all the data and shows it on the browser to find a solution, but I'm unsure on what to do with my if statement to isolate the strings and replace them. And since I'm using explode it is probably turning the data above into two separate arrays each, which further complicates things.

<?php

require 'includes/connect.php';

$pullkeywords = $db->query("SELECT keywords FROM main");

while ($result = $pullkeywords->fetch_object()) {

    $separatekeywords = explode(" ", $result->keywords);
    print_r ($separatekeywords);
    echo "<br />";
}

Any help is appreciated. Thank you in advance.

2
  • What is an example of one $result->keywords? BA 093, RJ 342, ES 324 or just BA 093??? Commented Aug 31, 2015 at 21:03
  • I have updated the question. Km 111 aracoiaba Araçoiaba sp 270 spvias vias sao paulo Araçoiaba Bidirecional is an example of the string I'm working with. Commented Aug 31, 2015 at 22:02

1 Answer 1

3

This regex should do it.

([A-Z]{2})\h(\d{3})

That says any character between A-Z two times ({2}). A horizontal white space \h. Then three {3} numbers \d. The ( and ) capture the values you want to capture. So $1 and $2 have the found values.

Regex101 Demo: https://regex101.com/r/nU2yN0/1

PHP Usage:

$string = 'BA 093, RJ 342, ES 324';
echo preg_replace('~([A-Z]{2})\h(\d{3})~', '$1-$2', $string);

Output:

BA-093, RJ-342, ES-324

You may want (?:^|\h)([A-Z]{2})\h(\d{3}) which would require the capital letters don't have text running into them. For example AB 345, cattleBE 123, BE 678. With this regex cattleBE 123 wouldn't be found. Not sure what your intent with this example is though so I'll leave that to you..

The ?: makes the () non capturing there. The ^ is so the capital letters can be the start of the string. The | is or and the \h is another horizontal space. You could do \s in place of \h if you wanted to allow new lines as well.

Update:

(?!KM)([A-Z]{2})\h(\d{3})

This will ignore strings starting with KM. https://regex101.com/r/nU2yN0/2

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, Chris! I'll try it out now. But in case I want to make a modified copy of the values instead of replacing them directly, how would I do it?
Also, I ran into some trouble: the space from the other keywords were also being removed and I need to add an exception. I want the pattern to be ignored if the first two characters start with "KM'. I apologize for the complications, I am only running into them now.
Okay then add (?!KM) to ([A-Z]{2})\h(\d{3}) so (?!KM)([A-Z]{2})\h(\d{3}). regex101.com/r/nU2yN0/2 What do you mean the space from the other keywords were also being removed and I need to add an exception. Are you using that explode still?
Let me get an example directly from the database so I can make this easier: Km 111 aracoiaba Araçoiaba sp 270 spvias vias sao paulo Araçoiaba Bidirecional. We want to add a sp-270 next to the sp 270. What's the best way to implement the REGEX you suggested?
Those aren't capital letters you could make it case insensitive, put an i after the delimiter (~). If that is what you want, that makes this open to more matches though.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.