trzczy
trzczy

Reputation: 1481

Regex to get capture group on the beginning, inside and end of string

The issue is shown online https://regex101.com/r/vW3vO8/1

I would like to make ma pattern better. It should get parts of the subject string that are divided by code fragments. In the example the code fragments are just php, js and html lines. They must divide.

regex:

(\[\/code\].*?\[code[^\]]*?\])

example string:

abc
[code lang='javascript']
    var a = 2.5;
[/code]
bcd
[code lang='php']
    $this->foo();
[/code]
cde
[code lang='html']
    <p clas = 'bar'><span>hello world</span></p>
[/code]
dez

So the regex gets

[/code]
bcd
[code lang='php']

and

[/code]
cde
[code lang='html']

This is ok but I would also need the parts on the beginning that would be this

abc
[code lang='javascript']

and on the and that would be this

[/code]
dez

Is it possible to achieve it via single regex pattern? Thank you

Upvotes: 0

Views: 41

Answers (1)

trincot
trincot

Reputation: 350127

You can perform a match with the start (^) and end ($) of the string in an OR (|) construction:

((^|\[\/code\]).*?(\[code[^\]]*?\]|$))

Regex tester

You might find this variant useful, which captures the same, except the tags:

(?<=^|\[\/code\])\s*(.*?)\s*(?=\[code[^\]]*?\]|$)

For the sample input, this matches:

  • abc
  • bcd
  • cde
  • def

Regex tester

Upvotes: 2

Related Questions