How to exclude a certain group from the regular expression

Asked

Viewed 2,983 times

6

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 erase a part, and the right one would be:

OCF|DINHEIRO|160.12

Then I started making a regular expression in Notepad++; In which I can locate exactly what I want to look for. The expression is this:

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

Now I need to know how to replace everyone to generate the value I expect:

OCF|DINHEIRO|160.12
  • Will do this by text editor or in some programming language?

  • AP uses Notepad++, so just use this Regular Expression with Ctrl+F, then go to the Replace tab, insert the expression in the Find box, change the search mode to Regular Expression and replace all.

2 answers

8

The idea is to search for the pattern you want to remove. 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 bar, it is necessary to escape because the bar in the regex has the function of OU
  • \d+ shortcut to search for numbers greedily.
  • $ edge looking at the end of a string.

Working

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

Regex101

7

If the expression is used in the editor Notepad++, do as the Marconi said.

Access the shortcut CTRL+H and in the field Find what? put the expression \|\d+$ and click on Replace Everything.

Notepad++

To use the expression you wrote, you will have to add at the end \|\d+ and in the field Replace with: place \1\2\3 which concerns the groups.

Notepad++

  • 1

    It was so cool gif's. :D +1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.