Search code examples
regexbulk-rename-utility

How to find a variable length string, using RegEx


I am trying to use a bulk rename tool to strip off an ID from migrated documents. I am struggling with the Regex.

Here is an example of some of the documents.

KIN-CHV-SAL-MAP-001 (ID 594).pptx
KIN-CHV-P18005-REP-001 R1 (ID 04).pdf

I basically need the Regex to find any instances of "(ID" then delete those characters and any other after it up the the ")". So the documents would look like this:

KIN-CHV-SAL-MAP-001.pptx
KIN-CHV-P18005-REP-001 R1.pdf

The amount of characters in-between the brackets varies and some documents do not have an ID. This is where i intend to use the RegEx.

Rename tool

Thank you for any help


Solution

  • You can use

    MATCH:   ^(.*?)\s*\(ID \d+\)
    REPLACE: \1
    Include Ext. must be UNCHECKED
    

    Pattern details

    • ^ - start of string
    • (.*?) - Group 1: any zero or more chars, as few as possible
    • \s* - one or more whitespace chars
    • \(ID \d+\) - ID, space, 1+ digits and ).

    The replacement is the contents of Group 1 (the string from the beginning till the (ID <<digits>>).

    See screenshot:

    enter image description here