Regex to match dynamic name once

Viewed 67

I want to match a company name once from a string that looks like this

What I want: Distribution Services Management only once

What I get CATE-N LUNA SI-N STELE SRL

What I'm trying Client\n+(.*?[ ]\s)

3 Answers

This might help you:

Client\n{2}.*\s{5}(.*)

My Logic is:

  1. The desired string is always after Client's down the next two-line, i.e. lies at third line.
  2. Before the start of our desired string there are some spaces(I assumed atleast 5)
  3. After those 5 spaces until the next newline is what our desired output lies.

Demo: https://regex101.com/r/GeGo3v/2

Using \s could also match a newline, and in the example data it looks like there are 2 or more spaces before the part that you want to match.

The reason you get capture CATE-N LUNA SI-N STELE SRL in group 1 is because you start the match after the newline without matching the part after the 2 spaces.

You could use

\bClient\n+.*[^\S\r\n]{2}(\S.*)

In parts, the pattern matches

  • \bClient\n+
  • .*[^\S\r\n]{2} Match until the end of the string, and backtrack to the last occurrence (or use \n+.*? for the first occurrence) of 2 whitespace chars except a newline
  • (\S.*) Capture group 1, match at least a single non whitespace char followed by optional chars except a newline

Regex demo

try this :

(?<=  \b).*(?=\n CIF)

DEMO

The way I solved this was to check the desired word between many spaces and the word CIF in the next line. I thought two spaces in (?<= \b) would be enough.

(?<= \b) part is to check at least two spaces and the word boundry. (?=\n CIF) part is positive lookahead assertion to check if it is before CIF in the next line.

Related