Home > Blockchain >  Regular expression for finding all dots on left side of string
Regular expression for finding all dots on left side of string

Time:06-22

I want to find all dots of each line but only on the left side of the "=". These are my samples:

"key.other.this.sample" = "I am the localized text."
"key.other.this.sample.with.more" = "I am the sample URL with www.sample.com"
"key.other.this.sample.with.more" = "I am the sample wit a \nline break"

I tried it with the following:

((\.{1})(?!")(?! )(?!(com))(?!(\n))).*(?<=(=))

This delivers me the right result but it checks the whole line instead of just checking the left side of the "=".

Can this achieved with "look behind"?

Thanks in advance.

CodePudding user response:

You can dot this with a lookbehind assertion, but only when an (in)finite quantifier is supported:

(?<=^[^=\n]*)\.

The pattern matches

  • (?<= Poaitive lookbehind, assert that what is to the left is
    • ^[^=\n]* Assert the start of the string, and the optionall match any character except a newline or an equals sign
  • ) Close the lookbehind
  • \. Match a dot

Regex demo

A pcre based pattern could be to skip all after the first equals sign

=.*(*SKIP)(*FAIL)|\.

Regex demo

CodePudding user response:

Use a look ahead for =:

\.(?=.*=)

See live demo working for all your examples.

The look ahead (?=.*=) asserts that there exists a = somewhere after the current position.


If you're expecting dots and equals after the dots all within the value, extend the look ahead to require 2 quotes after the equals:

\.(?=.*=.*".*")

See live demo.

This guards against:

"some.key" = "foo.bar=baz"
  • Related