Regular expression to match hyphenated words (kebab-case)

12.3k views Asked by At

How can I extract hyphenated strings from this string line?

ADW-CFS-WE CI SLA Def No SLANAME CI Max Outage Service

I just want to extract "ADW-CFS-WE" from it but has been very unsuccessful for the past few hours. I'm stuck with this simple regEx "(.*)" making the all of the string stated about selected.

4

There are 4 answers

10
phihag On
$input = "ADW-CFS-WE X-Y CI SLA Def No SLANAME CI Max Outage Service";
preg_match_all('/[A-Z]+-[A-Z-]+/', $input, $matches);
foreach ($matches[0] as $m) {
  echo $matches . "\n";
}

Note that this solutions assumes that only uppercase A-Z can match. If that's not the case, insert the correct character class. For example, if you want to allow arbitrary letters (like a and Ä), replace [A-Z] with \p{L}.

0
Patrick On

Just catch every space free [^\s] words with at least an '-'.

The following expression will do it:

<?php

$z = "ADW-CFS-WE CI SLA Def No SLANAME CI Max Outage Service";

$r = preg_match('#([^\s]*-[^\s]*)#', $z, $matches);
var_dump($matches);
0
mario On

You can probably use:

preg_match("/\w+(-\w+)+/", ...)

The \w+ will match one or more consecutive characters which may be letters, numbers or underscores (one word). And the second group ( ) will match one or more repetitions of: a hyphen followed by a sequence of one or more characters which may contain letters, numbers or underscores.

The trick with regular expressions is often specificity. Using .* will often match too much.

2
Anonymous On

The following pattern assumes the data is at the beginning of the string, contains only capitalized letters and may contain a hyphen before each group of one or more of those letters:

    <?php
    $str = 'ADW-CFS-WE CI SLA Def No SLANAME CI Max Outage Service';
    if (preg_match('/^(?:-?[A-Z]+)+/', $str, $matches) !== false)
        var_dump($matches);

Result:

    array(1) {
      [0]=>
      string(10) "ADW-CFS-WE"
    }