Search code examples
regexnintex-workflow

Grab first 4 characters of two words RegEx


I would like to grab the first 4 characters of two words using RegEx. I have some RegEx experinece however a search did not yeild any results.

So if I have Awesome Sauce I would like the end result to be AwesSauc


Solution

  • Use the Replace Text action with the following parameters:

    Pattern: \W*\b(\p{L}{1,4})\w*\W*
    Replacement text: $1

    See the regex demo.

    Pattern details:

    • \W* - 0+ non-word chars (trim from the left)
    • \b - a leading word boundary
    • (\p{L}{1,4}) - Group 1 (later referred to via $1 backreference) matching any 1 to 4 letters (incl. Unicode ones)
    • \w* - any 0+ word chars (to match the rest of the word)
    • \W* - 0+ non-word chars (trim from the right)