baao
baao

Reputation: 73221

Regex to replace character with character itself and hyphen

I need to replace some camelCase characters with the camel Case character and a -.

What I have got is a string like those:

Albert-Weisgerber-Allee 35
Bruninieku iela 50-10

Those strings are going through this regex to seperate the number from the street:

$data = preg_replace("/[^ \w]+/", '', $data);
$pcre = '\A\s*(.*?)\s*\x2f?(\pN+\s*[a-zA-Z]?(?:\s*[-\x2f\pP]\s*\pN+\s*[a-zA-Z]?)*)\s*\z/ux';
preg_match($pcre, $data, $h);

Now, I have two problems.

  1. I'm very bad at regex.

  2. Above regex also cuts every - from the streets name, and there are a lot of those names in germany and europe.

Actually it would be quite easy to just adjust the regex to not cut any hyphens, but I want to learn how regex works and so I decided to try to find a regex that just replaces every camel case letter in the string with

- & matched Camel Case letter

except for the first uppercase letter appearance.

I've managed to find a regex that shows me the places I need to paste a hyphen like so:

.[A-Z]{1}/ug     

https://regex101.com/r/qI2iA9/1

But how on earth do I replace this string:

AlbertWeisgerberAllee 

that it becomes

Albert-Weisgerber-Allee

Upvotes: 7

Views: 3617

Answers (3)

Anonymous
Anonymous

Reputation: 12017

You seem to be over complicating the expression. You can use the following to place - before any uppercase letters except the first:

(.)(?=[A-Z])

Just replace that with $1-. Essentially, what this regex does is:

  1. (.) Find any character and place that character in group 1.
  2. (?=[A-Z]) See if an uppercase character follows.
  3. $1- If matched, replace with the character found in group 1 followed by a hyphen.

Upvotes: 1

umka
umka

Reputation: 1655

To insert dashes before caps use this regex:

$string="AlbertWeisgerberAllee";
$string=preg_replace("/([a-z])([A-Z])/", "\\1-\\2", $string);

Upvotes: 7

karthik manchala
karthik manchala

Reputation: 13640

Just use capture groups:

(.)([A-Z])        //removed {1} because [A-Z] implicitly matches {1}

And replace with $1-$2

See https://regex101.com/r/qI2iA9/3

Upvotes: 1

Related Questions