Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting words after a specific character

Tags:

regex

I'm using a software to edit songs using regular expressions. This is what I have:

Jimmy Eat World - The Middle (.mp3)

What I would like to do is delete the space before the "-" and everything after, so I would just be left with "Jimmy Eat World"

And the other action that I would like to perform is to delete everything up to the "-" and the space following so I'd be left with just "The Middle"

like image 744
Mat B Avatar asked Jan 06 '13 16:01

Mat B


People also ask

How do I remove text after a special character in Excel?

Remove Text After a Character Using Find and Replace Copy and Paste the data from column A to column B (this is to keep the original data as well) With the cells in Column B selected, click on the Home tab. In the Editing group, click on the Find & Select option.

How do I delete everything after a character in Word?

In this case, to remove all before the > character, just search for "*> " (without the quotes) and replace with nothing. Then all characters before "> " are removed. The opposite is also easy. To remove everything after the > character, just search for ">*" (without the quotes) and replace with nothing.

How do I extract text before and after a specific character in Excel?

To get text following a specific character, you use a slightly different approach: get the position of the character with either SEARCH or FIND, subtract that number from the total string length returned by the LEN function, and extract that many characters from the end of the string.


1 Answers

That's an easy one.

First action - Remove anything after the dash:

  • Replace / -.*/ with the empty string. (Note there's an actual space before the dash.)
  • If there could be multiple spaces before the dash, you can use this variant: / +-.*/ (again with an actual space before the +).

Second action - Remove anything up to the dash:

  • Replace /.* - / with the empty string. (Note there's an actual space after the dash.)

Notes

  • The slashes / above are not part of the regex, you won't have to type them. They serve as a visual delimiter here.
  • The . means "any character" (except newlines, which you won't have anyway in filenames)
  • The * means "the previous item, zero to any number of times"
  • The + means "the previous item, at least once, possibly any number of times"
  • Most other characters in regular expressions mean what they say, so a space in the regex will match a space in your string. Notable exceptions are ^, $, ., +, *, ?, {, }, (, ), [, ], | and \, which have their own special meaning but are of no deeper concern in your situation.
like image 128
Tomalak Avatar answered Oct 21 '22 19:10

Tomalak