开发者

regex question redux regarding definition list

开发者 https://www.devze.com 2022-12-13 17:37 出处:网络
Trying to figure out a way to throw out attributes in this data that do not have any values. Thanks for helping.

Trying to figure out a way to throw out attributes in this data that do not have any values. Thanks for helping.

My current regex code , thanks to Tomalak looks like this

Regex find

([^=|]+)=([^|]+)(?:\||$)

Regex replace

<dt>$1</dt><dd>$2</dd>

Data looks like this

Bristle Material=|Wire Material=Steel|Dia.=4 in|Grit=|Bristle Diam=|Wire Size=0.0095 in|Arbor Diam=|Arbor Thread - TPI or Pitch=1/2 - 3/8 in|No. of Knots=|Face Width=1/2 in|Face Plate Thickness=7/16 in|Trim Length=7/8 in|Stem Diam=|Speed=6000 rpm [Max]|No. of Rows=|Color=|Hub Material=|Structure=|Tool Shape=|Applications=Cleaning rust, scale and dirt, Light Deburring, Edge Blending, Roughening for adhesion开发者_运维问答, Finish preparation prior to plating or painting|Applicable Materials=|Type=|Used With=Straight Grinders, Bench/Pedestal Grinders, Right Angle Grinders|Packing Type=|Quantity=1 per pack|Wt.=

End result should like this

 <dt>Wire Material</dt><dd>Steel</dd><dt>Dia.</dt><dd>4 in</dd><dt>Wire Size</dt><dd>0.0095 in</dd>

Not this

 Bristle Material=|<dt>Wire Material</dt><dd>Steel</dd><dt>Dia.</dt><dd>4 in</dd>Grit=|Bristle Diam=|<dt>Wire Size</dt><dd>0.0095 in


Here is how you can do it in PHP without using regular expressions:

$parts_list = explode('|', "Bristle Material=|Wire M....");
$parts      = "";

foreach( $parts_list as $part ){
    $p = explode('=', $part);
    if(!empty($p[1])) $parts .= "<dt>$p[0]</dt>\n<dd>$p[1]</dd>\n";
}

echo $parts;

And here is how you can do it with regular expressions:

$parts = preg_replace( 
    array('/([^=|]*)=(?:\||$)/','/([^=|]*)=([^|]+)(?:\||$)/'),
    array('', '<dt>$1</dt><dd>$2</dd>'),
    $inputString 
);

echo $parts;

Update

This is using a special replace feature of the PHP preg_replace which takes an array of regex expressions, and an array of replacement strings. The array() syntax of the function basically equates to this:

If I can match this: /([^=|]*)=(?:\||$)/ then replace it with an empty string.
If I can match this: /([^=|]*)=([^|]+)(?:\||$)/ then replace it with <dt>$1</dt><dd>$2</dd>

To test it in a Regex editor, you would run the first expression first (/([^=|]*)=(?:\||$)/) then run the second expression on the result of the first expression.


([^=|]*)=([^|]*)(?:\||$)

to skip the ones with out a value, try this:

(?:[^=|]*=|([^=|]*)=([^|]+))(?:\||$)


looks like you want preg_match here rather than preg_replace

 preg_match_all('~([^|]+)=([^|\s][^|]*)~', $str, $matches, PREG_SET_ORDER);
 foreach($matches as $match)
      echo "<dt>{$match[1]}</dt><dd>{$match[2]}</dd>\n";
0

精彩评论

暂无评论...
验证码 换一张
取 消