简体   繁体   中英

Regular expression to match hyphenated words

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.

You can probably use:

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

The \\w+ will match any number of alphanumeric characters (= one word). And the second group ( ) is any additional number of hyphen with letters.

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

$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 AZ 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 [AZ] with \\p{L} .

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);

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"
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM