Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
Regex String Group Different Status

in this case I create a regex for the group in code,price,name,status but the last line does not match, I want the last line with status Y if there is no word [Y]

Harga: 
X25=2.335NAME1 25 [X];
X1=5.720NAME2 5 [Y];
X2=10.720NAME3 10 [Y];
X4=4.200NAME4 4;
(?<code>.*)\=(?<price>[0-9]{1,3}\.?[0-9]{1,3})(?<name>.*)\s(?:\[(?<status>X|Y)\])
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can change the first .* to a negated character class matching any char except =

The name group can be changed to a negated character class matching any char other than [ and ]

Using [0-9]{1,3}\.?[0-9]{1,3} matches at least 2 digits. If you also want to match a single digits, you can make the . and the next 1-3 digits optional

The last part can be made optional including the whitespace char to also match the last line

(?<code>[^=\n]*)=(?<price>[0-9]{1,3}(?:\.[0-9]{1,3})?)(?<name>[^\][]*?)(?:\s*\[(?<status>[XY])\])?;

The pattern matches:

  • (?<code>[^=\n]*)= Group code match optional chars other than =, then match =
  • (?<price> Group price
    • [0-9]{1,3}(?:\.[0-9]{1,3})? Match 1-3 digits and optionally . and 1-3 digits
  • ) Close gropu
  • (?<name>[^\][]*?) Group name, optionally match any char except [ and ] as least as possible
  • (?: Non capture group to make the whole part optional
    • \s*\[(?<status>[XY])\] Optionally match whitespace chars, group status matching either X or Y using a character class between square brackets
  • )?; Close the non capture group, and match the ; at the end

Regex demo

If the ; at the end is not mandatory, but it can not occur in between as well, you can exclude matching it as in this regex demo.

const regex = /(?<code>[^=\n]*)=(?<price>[0-9]{1,3}(?:\.[0-9]{1,3})?)(?<name>[^\][]*?)(?:\s*\[(?<status>[XY])\])?;/g;
const str = `Harga: 
X25=2.335NAME1 25 [X];
X1=5.720NAME2 5 [Y];
X2=10.720NAME3 10 [Y];
X4=4.200NAME4 4;`;
let m;

console.log([...str.matchAll(regex)].map(m => m.groups));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda