Search code examples
regexregex-negationregex-lookaroundsregex-groupregex-greedy

RegEx for matching any char except numbers and defined words


I'm trying to batch rename my files using Advanced Renamer, that support regex.

I want to match all characters to exclude those and create a new filename, but maintain numbers of episodes and specifics words to differentiate regular episodes that OVAs, for example.

I tried \D[^OVA] and others similar but nothing works:

Hunter x Hunter 02 = 02

Hunter x Hunter OVA 08 = OVA 08

Hunter x Hunter OVA Greed Island Final 01 = OVA Island 01


Solution

  • I'm guessing that you wish to exclude OVA and all your numbers. Then, this expression may help you to do so or design one:

    ([^OVA0-9]+)*
    

    enter image description here

    Graph

    This graph shows how the expression would work and you can visualize other expressions in this link:

    enter image description here


    If you wish to add a list of word to exclude, this expression may do so:

    ([\S\s]*?)([0-9]+|OVA|Any|Thing|Else|That|You|Like)
    

    You can add any other word that you might want using an | to exclude.

    enter image description here

    RegEx Descriptive Graph

    enter image description here

    JavaScript Test

    const regex = /([\S\s]*?)([0-9]+|OVA|Any|Thing|Else|That|Like)/gm;
    const str = `Hunter x Hunter VOA Greed Island Final 01 = OVA Island 01`;
    let m;
    
    while ((m = regex.exec(str)) !== null) {
        // This is necessary to avoid infinite loops with zero-width matches
        if (m.index === regex.lastIndex) {
            regex.lastIndex++;
        }
        
        // The result can be accessed through the `m`-variable.
        m.forEach((match, groupIndex) => {
            console.log(`Found match, group ${groupIndex}: ${match}`);
        });
    }