I am trying to replace a series of plane codes that are strings in PHP.
For example, 757-252 string should become a 752 string
It takes the first two digits before the hypen, and the first digit after the hyphen.
There are three conditions that make this complicated where I can't just use explode and I think need to use RegEx:
- The digit before the hyphen may be more than just a single digit, and it MAY be alpha numeric.
- The last three digits in the example above is a variable length and can be alpha or numeric.
- There are values that DON'T fit this pattern that I need to be left alone (i.e. DHC-8-103)
Any thoughts
RegEx - Working at RegExPal.com
7[0-9][0-9a-zA-Z]+\-[0-9a-z-A-Z][0-9a-zA-Z]+
PHP Attempt to Replace Not Working
$aircraft = '757-252';
$pattern = '/(7)(0-9)(0-9a-zA-Z)+-(0-9a-zA-Z)(0-9a-zA-Z)+/';
$replacement = '\\1\\2\\4';
$aircraft_code = preg_replace($pattern , $replacement , $aircraft);
[ ]brackets from your groups, so they aren't functioning as ranges in character classes like you expect. Instead, you get the literal string "0-9", for example.