Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for remove everything after | (with | )

I was trying to find a solution for my problem.

Input:

This is the sample title | mypcworld 

Output:

This is the sample title 

I want to remove everything comes after " | " using simple regex on notepad++ It seems to be really simple one. But I was tried with some other regex "|.*$" listed on here, but no luck. The problem was the character "|" was alteration equivalent of or. So please help me to solve it.

like image 531
Alex Ms Avatar asked Oct 14 '13 19:10

Alex Ms


People also ask

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.

How do you delete everything after a certain character?

Press Ctrl + H to open the Find and Replace dialog. In the Find what box, enter one of the following combinations: To eliminate text before a given character, type the character preceded by an asterisk (*char). To remove text after a certain character, type the character followed by an asterisk (char*).

What does \+ mean in regex?

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. Example: "[^0-9]" matches any non digit.

How do I delete everything after Notepad ++?

If you want to delete all the text after a character or string (to the right) in Notepad++ you would need to make use of regex. So, simply add . * to delete all characters after the string or character on each that you want to delete from a line.


2 Answers

The pipe, |, is a special-character in regex (meaning "or") and you'll have to escape it with a \.

Using your current regex:

\|.*$ 

I've tried this in Notepad++, as you've mentioned, and it appears to work well.

like image 95
newfurniturey Avatar answered Sep 20 '22 22:09

newfurniturey


If you want to get everything after | excluding set character use this code.

[^|]*$ 

Others solutions \|.*$

Results : | mypcworld 

This one [^|]*$

Results : mypcworld 

http://regexr.com/3elkd

like image 38
Felipe Lopez Avatar answered Sep 21 '22 22:09

Felipe Lopez