regexsedalphanumericpositive-lookahead

Sed replace string on alphanumeric with certain length that must contain one capitalized letter and one number


I want to do a string replacement on any string that is surrounded by a word boundary that is alphanumeric and is 14 characters long. The string must contain at least one capitalized letter and one number. I know (I think I know) that I'll need to use positive look ahead for the capitalized letter and number. I am sure that I have the right regex pattern. What I don't understand is why sed is not matching. I have used online tools to validate the pattern like regexpal etc. Within those tools, I am matching the string like I expect.

Here is the regex and sed command I'm using.

\b(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]{14}\b

The sed command I'm testing with is

echo "asdfASDF1234ds" | sed 's/\b(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]{14}\b/NEW_STRING/g'

I would expect this to match on the echoed string.


Solution

  • sed doesn't support lookaheads, or many many many other modern regex Perlisms. The simple fix is to use Perl.

    perl -pe 's/\b(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]{14}\b/NEW_STRING/g' <<< "asdfASDF1234ds"