Skip to main content
added 649 characters in body
Source Link
Stéphane Chazelas
  • 586.9k
  • 96
  • 1.1k
  • 1.7k

Since you're using perl regexp operators, why not just use perl?

perl -pe 's/\bclass\s+\K\S+/RequiredContent/g' < your-file
  • \b is a word boundary operator, it will match as long as there's no word character before class.
  • \s matches any whitespace character (limited to ASCII ones as we did not tell perl to decode the input in the user's locale or UTF-8¹, so the input is interpreted as if encoded in ISO8859-1 (thankfully the 0xa0 byte, ISO8859-1's non-breaking space encoding not being considered whitespace as long as you don't add the u flag to that substitution).
  • \K resets the start of the matched string, so only what's matched after that will be replaced, so you don't need to capture what's before to add it back in the replacement.
  • \S is any non-whitespace. In perl, that's equivalent to [^\s] while in sed, [^\s] would match any character other than \ and s.

¹ assuming the PERL_UNICODE environment variable is not set

Since you're using perl regexp operators, why not just use perl?

perl -pe 's/\bclass\s+\K\S+/RequiredContent/g' < your-file

Since you're using perl regexp operators, why not just use perl?

perl -pe 's/\bclass\s+\K\S+/RequiredContent/g' < your-file
  • \b is a word boundary operator, it will match as long as there's no word character before class.
  • \s matches any whitespace character (limited to ASCII ones as we did not tell perl to decode the input in the user's locale or UTF-8¹, so the input is interpreted as if encoded in ISO8859-1 (thankfully the 0xa0 byte, ISO8859-1's non-breaking space encoding not being considered whitespace as long as you don't add the u flag to that substitution).
  • \K resets the start of the matched string, so only what's matched after that will be replaced, so you don't need to capture what's before to add it back in the replacement.
  • \S is any non-whitespace. In perl, that's equivalent to [^\s] while in sed, [^\s] would match any character other than \ and s.

¹ assuming the PERL_UNICODE environment variable is not set

Source Link
Stéphane Chazelas
  • 586.9k
  • 96
  • 1.1k
  • 1.7k

Since you're using perl regexp operators, why not just use perl?

perl -pe 's/\bclass\s+\K\S+/RequiredContent/g' < your-file