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

183
Visualizações
Regex to avoid specific content

I have a string like 23DGERA@SPK_20W L+R FA-2@1+342HSHC@CPU_8PIN INTEL_TEST!@1+2356GHMX@SSD_256G MICRON_CONTENT@2 + blablabla.

What I would like to do is to split up the string by +, yet in SPK section there is a L+R that would interrupt the process. Is there any REGEX that could achieve what I want?

In result should be:

23DGERA@SPK_20W L+R FA-2@1
342HSHC@CPU_8PIN INTEL_TEST!@2
2356GHMX@SSD_256G MICRON_CONTENT@2

and now what i always get:

23DGERA@SPK_20W L
R FA-2@1
342HSHC@CPU_8PIN INTEL_TEST!@2
2356GHMX@SSD_256G MICRON_CONTENT@2

I'm using Javascript .split('+') by now.

Any help will be appretiated.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use a matching regex solution:

text.match(/(?:L\+R|[^+])+/g)

See the regex demo. Details:

  • (?: - start of a non-capturing group:
    • L\+R - L+R string
    • | - or
    • [^+] - any char other than +
  • )+ - end of the group, one or more occurrences.

See the JavaScript demo:

var text = '23DGERA@SPK_20W L+R FA-2@1+342HSHC@CPU_8PIN INTEL_TEST!@1+2356GHMX@SSD_256G MICRON_CONTENT@2';
console.log(text.match(/(?:L\+R|[^+])+/g));

ECMAScript 2018+ compliant solution

In case you want to migrate to a more modern ECMAScript flavor, you can use

text.split(/\+(?<!L\+(?=R))/)

This will match a + that is not part of an L+R string.

const text = '23DGERA@SPK_20W L+R FA-2@1+342HSHC@CPU_8PIN INTEL_TEST!@1+2356GHMX@SSD_256G MICRON_CONTENT@2';
console.log(text.split(/\+(?<!L\+(?=R))/));

See the regex demo.

about 4 years ago · Juan Pablo Isaza Relatório

0

Instead of splitting on a + you could match the format in the example data.

First match a part containing a single @, and then match till the first occurrence of @ followed by a digit.

Note that the second match will be 342HSHC@CPU_8PIN INTEL_TEST!@1 instead of 342HSHC@CPU_8PIN INTEL_TEST!@2

\w+@\w+ [^@]*@\d\b

The pattern matches:

  • \w+@\w+ Match 1+ word characters, @ and at 1+ word characters
  • [^@]*@ Match a space, optional chars other than @, then match @
  • \d\b Match a digit and a word boundary to prevent a partial match

Regex demo

const s = "23DGERA@SPK_20W L+R FA-2@1+342HSHC@CPU_8PIN INTEL_TEST!@1+2356GHMX@SSD_256G MICRON_CONTENT@2 + blablabla";
const regex = /\w+@\w+ [^@]*@\d\b/g;
console.log(s.match(regex));

about 4 years ago · Juan Pablo Isaza Relatório

0

The string looks like a list of parts each with a quantity e.g. @1. You can use that to identify the correct + characters to split on.

Using a look-behind containing @\d+ -> (?<=@\d+) followed by the character you want to match (escaped because + has a special meaning) gives:

(?<=@\d+)\+

Using this in code we also need specify the g modifier to match all instances instead of just the first one.

const str =  '23DGERA@SPK_20W L+R FA-2@1+342HSHC@CPU_8PIN INTEL_TEST!@1+2356GHMX@SSD_256G MICRON_CONTENT@2'
const items = str.split(/(?<=@\d+)\+/g);

console.log(items);

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