Skip to content
Advertisement

grab specific field value from the string using regex

I’ve a text file, from that I have extracted these two paragraph block. The text example is given below:

Text Example:

NOMEAR ISABELLE FERREIRA ZARONI, ID FUNCIONAL Nº 5100796-7, para exercer, com validade a contar de 16 de novembro de 2020, o cargo em comissão de Assessor, símbolo DAS-7, da Sub- secretaria de Concessões e Parcerias, da Secretaria de Estado de Planejamento e Gestão, anteriormente ocupado por Vinicius dos San- tos Silva, ID Funcional n° 5108029-0. Processo nº SEI- 1 2 0 0 0 1 / 0 1 4 6 11 / 2 0 2 0 .

NOMEAR KARINE MATOS DIAS, ID FUNCIONAL Nº 5092869-4 para exercer, com validade a contar de 16 de novembro de 2020, o cargo em comissão de Assessor, símbolo DAS-7, da Secretaria de Estado de Planejamento e Gestão, anteriormente ocupado por Amauri Ferrei- ra do Carmo, ID Funcional nº 5099579-0. Processo nº SEI- 1 2 0 0 0 1 / 0 1 4 6 11 / 2 0 2 0 .

From the above text block I want to grab the bold values only from each paragraph as a individual row.

What I have tried:

JavaScript

My Current Output:

JavaScript

My current output is almost ok but having issue with multiple replace() and some time this static replace is breaking my code also. So is there other way I can achieve that using regex matching on those bold text?

Advertisement

Answer

To get the values in bold, you might use 3 capturing groups with an alternation:

JavaScript

In parts

  • b A word boundary to prevent the word being part of a longer word
  • (?: Non capture group
    • (?:NOMEAR|d[ea]|por) Match one of NOMEAR de da por
    • ([^,]+?) Capture group 1, match any char except , non greedy
    • (?: e Gestão)?, Optionally match e Gestão and match a ,
    • | Or
    • ([A-Zd]+-d+) Capture in group 2 matching 1+ times either A-Z or a digit and - and 1+ digits
    • | Or
    • SEI- ([d /]+)b Match SEI- , capture in group 3 one of the listed followed by a word boundary
  • ) Close non capture group

Regex demo | Python demo

For example

JavaScript

Output

JavaScript
Advertisement