regex – How to exclude a certain group from regular expression

Question:

I have a tax file that comes with the following information on the line:

Ex:. OCF|DINHEIRO|160.12|12

To fix it I have to delete a part, and the right thing would be:

OCF|DINHEIRO|160.12

So I started doing a regular expression in notepad++; In which I can find exactly what I want to look for. The expression is this:

(^OCF\|)(\w*\|)(\d*.\d*)

Now I need to know how to replace them all so that the value I expect is generated:

OCF|DINHEIRO|160.12

Answer:

The idea is to look for the pattern you want to remove. As I understand you want to remove the |12 at the end of the text, that is, the pattern you are looking for is |número

My expression is based on the following:

  • \| look for a slash, escape is needed because slash in regex has OU function
  • \d+ shortcut to search for numbers in a greedy way.
  • $ border that looks for the end of a string.

Working

 let texto = 'OCF|DINHEIRO|160.12|12'; let expressao = /\|\d+$/; console.log(texto.replace(texto.match(expressao),''));

Regex101

Scroll to Top