Home > Mobile >  Replace all word boundaries excluding certain patterns with an asterisk
Replace all word boundaries excluding certain patterns with an asterisk

Time:12-28

I'm working with enter image description here

Thanks

CodePudding user response:

I assume that you wish to add an asterisk to the end of each word that has a length of at least 4 that does not already end with an asterisk and is not within a string delimited with double-quotes.

Assuming your strings are well-formed in the sense that the number of double-quotes is even, you can replace the content of capture group 1 of matches of the following regular expression with an asterisk appended to the content of capture group 1:

(\w{4,})(?!\w|\*|[^"]*"(?:(?:[^"]*"{2}))*[^"]*$)

enter image description here

With the advice of @The fourth bird, you can improve the regex deleting (?<!")

(?<=\w)\b(?!\s[\w] ["*])(?!["*])
  • Related