Regular expression pattern without one case

I would like to remove some lines from the filename. I want to remove every line in the parenthesis, but not if there is a "remix" or "Remix" or "REMIX" line. Now I have

sed "s/\s*\(\s?[A-z0-9. ]*\)//g"

but how to exclude cases when there is a remix in the line?

+3


source to share


3 answers


You can use a capture group:

sed 's/\(\s*([^)]*remix[^)]*)\)\|\s*(\s\?[a-z0-9. ]*)/\1/gi'

      

When the "remix branch" does not match, the capture group is undefined and the corresponding portion is replaced with an empty string.

When the "remix branch" succeeds, the matched portion is replaced with the contents of the capture group, therefore on its own.

Note: if this helps to avoid false positives, you can add word boundaries around the "remix": \bremix\b

more details:

\(           # open the capture group 1
    \s*      # zero or more white-spaces
    (        # a literal parenthesis
    [^)]*    # zero or more characters that are not a closing parenthesis
    remix
    [^)]*
    )   
\)           # close the capture group 1
\|           # OR
# something else between parenthesis

\s*  # note that it is essential that the two branches are able to
     # start at the same position. If you remove \s* in the first
     # branch, the second branch will always win when there a space
     # before the opening parenthesis.
(\s\?[a-z0-9. ]*)

      

\1

- link to capture group 1

i

makes the template case insensitive

[EDIT]

If you want to do this in a POSIX-compliant way, you have to take a different approach because several Gnu features are not available, notably interleaving \|

(but also modifier i

, character \s

class, optional quantifier \?

).



This other approach is to find all possible characters that are not an open parenthesis and all possible substrings enclosed between parentheses, with a "remix" inside followed by possible white spaces and a possible substring enclosed between parentheses.

As you can see, everything is optional and the pattern can match an empty string, but that's not a problem.

Everything before the part of the bracket to be removed will be written to group 1.

sed 's/\(\([^(]*([^)]*[Rr][Ee][Mm][Ii][Xx][^)]*)[^ \t(]*\([ \t]\{1,\}[^ \t(]\{1,\}\)*\)*\)\([ \t]*([^)]*)\)\{0,1\}/\1/g;'

      

more details:

\(     # open the capture group 1
    \(
        [^(]*  # all that is not an opening parenthesis
        # substring enclosed between parenthesis without "remix"
        ( [^)]* [Rr][Ee][Mm][Ii][Xx] [^)]* )

        # Let reach the next parenthesis without to match the white-spaces
        # before it (otherwise the leading white-spaces are not removed)
        [^ \t(]*  # all that is not a white-space or an opening parenthesis
        # eventual groups of white-spaces followed by characters that are
        # not white-spaces nor opening parenthesis
        \( [ \t]\{1,\} [^ \t(]\{1,\} \)*
    \)*
\)     # close the capture group 1
\(
    [ \t]*  # leading white-spaces
    ([^)]*) # parenthesis
\)\{0,1\}   # makes this part optional (this avoid to remove a "remix" part
            # alone at the end of the string)

      

Word boundaries are also not available in this mode. Thus, the only way to imitate them is by listing four possibilities:

([Rr][Ee][Mm][Ii][Xx])                # poss1
([Rr][Ee][Mm][Ii][Xx][^a-zA-Z][^)]*)  # poss2
([^)]*[^a-zA-Z][Rr][Ee][Mm][Ii][Xx])  # poss3
([^)]*[^a-zA-Z][Rr][Ee][Mm][Ii][Xx][^a-zA-Z][^)]*) # poss4

      

and replace ([^)]*[Rr][Ee][Mm][Ii][Xx][^)]*)

with:

\(poss1\)\{0,\}\(poss2\)\{0,\}\(poss3\)\{0,\}\(poss4\)\{0,\}

      

+5


source


Just skip the lines corresponding to "remix":



sed '/([^)]*[Rr][Ee][Mm][Ii][Xx][^)]*)/! s/([^)]*)//g'

      

+1


source


where parenthesis (US): []

sed '/remix\|REMIX\|Remix/ !s/\[[^]]*]//g'

      

where bracet (ROW): ()

sed '/remix\|REMIX\|Remix/ !s/([^)]*)//g'

      

provided: - no inner parenthesis - Another form of remix is ​​selected ( ReMix

, ...), so the line is removed - Remix can be any place in the header ( i love remix

) [if necessary, specify what to take and remove]

0


source







All Articles