Prakash Patani
Prakash Patani

Reputation: 547

How to split specific pattern using regex

I have below string

Dim strTemplate as string = 
"<table>
  <tr>
       <td>Name</td>
       <td>Address</td>
       <td>City</td>   
  </tr>
  <tr>
       <td>[%Name%]</td>
       <td>[%Address%]</td>
       <td>[%City%]</td>   
  </tr>
</table>"

Dim strSplits = New List(Of String)(Regex.Split(strval, "REGEXRequired"))

Now i want to write regex with split above string and give only pattern string [%...%].

i.e. want [%Name%], [%Address%], [%City%] values in strSplits list.

any suggestion or help will be appreciated .

Upvotes: 0

Views: 60

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336418

Why would you use Split() for that?

Dim strSplits = New List(Of String)(Regex.Matches(strval, "\[%.*?%\]"))

is much easier, isn't it?

Upvotes: 3

aelor
aelor

Reputation: 11116

this will get you going

/(\[%.*?%\])/g

demo here : http://regex101.com/r/kV2rK4

Upvotes: 1

Related Questions