2

I am stuck on a Regex problem. Sample Text

data-key=foo1,
data-key=foo2,
data-key=foo3,
BAR,
data-key=foo4,
asd
data-key=foo5,
asfda
data-key=foo6,

I now want all data-key lines after the word "BAR". Desired Result:

data-key=foo4,
data-key=foo5,
data-key=foo6,

This RegEx would give me the result I want, but I don´t want to specify how many times the data-key line occurs, as it could be any number:

(?s)BAR.*(data-key.*?,).*(data-key.*?,).*(data-key.*?,)

Any ideas?

  • 5
    What language or tool are you using regex in? – Sweeper Dec 03 '19 at 15:11
  • If you regex engine supports variable-width lookbehinds (n.b. : few engines do) you could use `(?<=BAR.*)data-key.*?,` ([sample](http://regexstorm.net/tester?p=%28%3f%3c%3dBAR.*%29data-key.*%3f%2c&i=data-key%3dfoo1%2c%0d%0adata-key%3dfoo2%2c%0d%0adata-key%3dfoo3%2c%0d%0aBAR%2c%0d%0adata-key%3dfoo4%2c%0d%0aasd%0d%0adata-key%3dfoo5%2c%0d%0aasfda%0d%0adata-key%3dfoo6%2c&o=s)) – Aaron Dec 03 '19 at 15:25
  • will there always be exactly one garbage line in between the lines you actually wish to extract? – CyberStems Dec 03 '19 at 15:50

2 Answers2

3

With any regex flavor at least supporting lookaheads to check BAR is not ahead.

(data-key[^,\n]*,).*(?![\s\S]*?\nBAR)

See this demo at regex101  (using a capturing group to extract the part until comma)


If using PCRE, there is (*SKIP)(*F) available to skip some part.

(?m)(?s:\A.*?^BAR)(*SKIP)(*F)|^data-key.*?,
  • (?m) flag for multline mode to make the caret match line start and the dollar line end
  • \A matches start of the string
  • (?s: starts a non capturing group with dotall flag to make the dot match newlines

See another demo at regex101

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
1

Unless you're asking this for theoretical reasons, I'd suggest just splitting your input by BAR and then executing a regexp on everything after it:

var str="data-key=foo1,\ndata-key=foo2,\ndata-key=foo3,\nBAR,\ndata-key=foo4,\nasd\ndata-key=foo5,\nasfda\ndata-key=foo6";

var regex = /(?:data-key=([^,]*))/ig;
 
var matches = Array.from(str.split("BAR,")[1].matchAll(regex));

console.log(matches);

This gives you the result you're looking for.

Pavel Lint
  • 3,252
  • 1
  • 18
  • 18