1

I have a string like this:

[text="This is the title"]This is the content of the title[/text]

And I'm using regex to take values, such as This is the title, but mostly This is the content of the title... Of this I can not do it.

Use the following syntax regex:

/\[text\s*\=\s*\"([^\"]*)\"\]/i

But then I do not know how to take all the text until it finds the word [/text].

Thanks to anyone who helps me.

mikelplhts
  • 1,181
  • 3
  • 11
  • 32

1 Answers1

1

You can use the following:

\[(text)\s*=\s*"([^"]*)"\](.*?)\[\/\1\]

Explanation:

  • \[(text)\s*=\s*"([^"]*)"\] you know what it means..
  • (.*?) capture all characters in a non greedy way
  • \[\/\1\] followed by literal [/ and then backreference to first capture group (text) and ]

And extract values like This is the title with $2 and values like This is the content of the title with $3

See DEMO

karthik manchala
  • 13,492
  • 1
  • 31
  • 55
  • Thanks for the help, now I understand where he was wrong. The only thing I noticed you used the `?`, but can not figure out what it does, because if you remove it all works the same. However, thanks again. – mikelplhts May 21 '15 at 17:44
  • @MicheleLapolla `?` means non greedy match.. it will stop once it encounters `[` otherwise it would match everything till the end.. here you may not notice the difference because of backtracking.. you can experiment it by adding `blablabla` at the end of your string.. Happy to help :) – karthik manchala May 21 '15 at 17:51
  • You mean like this? [link](https://regex101.com/r/yM1dM7/3). Maybe I misunderstood, but does not change anything anyway. Sorry for disturbing! – mikelplhts May 21 '15 at 18:14
  • 1
    @MicheleLapolla glad that you are experimenting.. check this [link](https://regex101.com/r/yM1dM7/4) – karthik manchala May 21 '15 at 18:18