Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for Replacing everything after instance of a character

I have a large quantity of data in the following format:-

EXPERTISE_JOBTITLECOOKIE = "JobTitleCookie";
EXPERTISE_POSITIONCOOKIE = "PositionCookie";
EXPERTISE_QUALIFICATIONSPROFESSIONALMEMBERSHIPSCOOKIE = "QualificationsProfessionalMembershipsCookie";
EXPERTISE_YEARSOFEXPERIENCECOOKIE = "YearsOfExperienceCookie";
EXPERTISE_ADDITIONALCOMMENTSCOOKIE = "AdditionalCommentsCookie";

I'm looking for some regex that could be used to replace all characters after the = characters, to leave only the variable name in capitals.

I tried:

^\*\d+$

but this isn't picked up. I'm also looking for regex for the same problem that works in Sublime Text 2.

Could someone explain what the above does, and what I might be looking for in a RegEx statement?

Thanks

like image 446
Mike Upjohn Avatar asked Dec 23 '15 14:12

Mike Upjohn


People also ask

How do I replace everything after?

Select the list you will replace all after/before the specific characters, and press the Ctrl + H keys simultaneously to open the Find and Replace dialog box. Note: You can also open the Find and Replace dialog box by clicking Home > Find & Select > Replace.

What does \d mean in regex?

In regex, the uppercase metacharacter is always the inverse of the lowercase counterpart. \d (digit) matches any single digit (same as [0-9] ). The uppercase counterpart \D (non-digit) matches any single character that is not a digit (same as [^0-9] ).

What does \+ mean in regex?

Example: The regex "aa\n" tries to match two consecutive "a"s at the end of a line, inclusive the newline character itself. Example: "a\+" matches "a+" and not a series of one or "a"s. ^ the caret is the anchor for the start of the string, or the negation symbol. Example: "^a" matches "a" at the start of the string.

How do you delete everything after regex?

Regex Replace We can also call the string replace method with a regex to remove the part of the string after a given character. The /\?. */ regex matches everything from the question to the end of the string. Since we passed in an empty string as the 2nd argument, all of that will be replaced by an empty string.


2 Answers

Could someone explain what the ^\*\d+$ does

  • ^ - Anchor that indicates the start of the line
  • \* - The * character literally (since it's escaped)
  • \d+ - One or more digits [0-9]
  • $ - Anchor that indicates the end of the line

In other words, ^\*\d+$ will match a line such as *1 or *10000.


You are looking for a regular expression to capture the characters until the = character:

^(.*?) =.*$

This will capture the characters before a space followed by the = character, and the match all of the remaining characters on that line so that they can be replaced.

If you want to remove everything after the capturing group, just replace with \1 as demonstrated by this example here.

If you want to modify the string after the = character, replace with \1 = "Some String", as demonstrated by this example here.

like image 78
Josh Crozier Avatar answered Oct 24 '22 19:10

Josh Crozier


I'd do:

Type Ctrl+H

Find what: =.+$
Replace with: NOTHING

Type Replace all

Be sure you have checked Regular Expression but NOT . matches newline.

This will replace everything after = (included) by nothing.

like image 35
Toto Avatar answered Oct 24 '22 19:10

Toto