Home > Mobile >  Character not at begining of line; not followed or preceded by character
Character not at begining of line; not followed or preceded by character

Time:06-28

I'm trying to isolate a " character when (simultaneously):

  • it's not in the beginning of the line
  • it's not followed by the character ";"
  • it's not preceded by the character ";"

E.g.:

Line: "Best Before - NO MATCH

Line: Best Before"; - NO MATCH

Line: ;"Best "Before - NO MATCH

Line: Best "Before - MATCH

My best solution is (?<![;])([^^])(")(?![;]) but it's not working correctly.

I also tried (?<![;])(")(?![;]), but it's only partial (missing the "not at the beginning" part)

I don't understand why I'm spelling the "AND not at the beginning" wrong. Where am I missing it?

CodePudding user response:

If you want to allow partial matches, you can extend the lookbehind with an alternation not asserting the start of the string to the left.

The semi colon [;] does not have to be between square brackets.

 (?<!;|^)"(?!;)

Regex demo

if you want to match the " when there is no occurrence of '" to the left and right, and a infinite quantifier in a lookbehind assertion is allowed:

(?<!^.*;(?=").*|^)"(?!;|.*;")

Regex demo

CodePudding user response:

You can use the fact that not preceded by ; means that it's also not the first character on the line to simplify things

[^;]"(?:[^;]|$)

This gives you

Match a character that's not a ; (so there must be a character and thus the next character can't be the start of the line)

Match a "

Match a character that's not a ; or the end of the line

CodePudding user response:

I know you are asking for a regex solution, but, almost always, strings can also be filtered using string methods in whatever language you are working in.

For the sake of completeness, to show that regex is not your only available tool here, here is a short javascript using the string methods:

  • myString.charAt()
  • myString.includes()

Working Example:

const checkLine = (line) => {
  
  switch (true) {
  
    // DOUBLE QUOTES AT THE BEGINNING
    case(line.charAt(0) === '"') :
      return console.log(line, '// NO MATCH');
    
    // DOUBLE QUOTES IMMEDIATELY FOLLOWED BY SEMI-COLON
    case(line.includes('";')) :
      return console.log(line, '// NO MATCH');
    
    // DOUBLE QUOTES IMMEDIATELY PRECEDED BY SEMI-COLON
    case(line.includes(';"')) :
      return console.log(line, '// NO MATCH');
    
    default:
      return console.log(line, '// MATCH');
  }
}


checkLine('"Best Before');

checkLine('Best Before";');

checkLine(';"Best "Before');

checkLine('Best "Before');


Further Reading:

  • Related