Home > OS >  Regex to avoid specific content
Regex to avoid specific content

Time:11-09

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.

CodePudding user response:

You can use negative lookbehind to exclude when preceded by L

const input = "23DGERA@SPK_20W L R FA-2@1 342HSHC@CPU_8PIN INTEL_TEST!@1 2356GHMX@SSD_256G MICRON_CONTENT@2   blablabla";

console.log(input.split(/(?<!L)\ /))
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

or negative lookahead to exclude followed by R

const input = "23DGERA@SPK_20W L R FA-2@1 342HSHC@CPU_8PIN INTEL_TEST!@1 2356GHMX@SSD_256G MICRON_CONTENT@2   blablabla";

console.log(input.split(/\ (?!R)/))
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

or combination of both:

const input = "23DGERA@SPK_20W L R FA-2@1 342HSHC@CPU_8PIN INTEL_TEST!@1 2356GHMX@SSD_256G MICRON_CONTENT@2   blablabla"

console.log(input.split(/(?<!L)\ (?!R)/))
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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));
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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))/));
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

See the regex demo.

CodePudding user response:

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);
<iframe name="sif6" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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));
<iframe name="sif7" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related