如何根据模式转换线条?

How to convert lines on the basis of pattern?

我有需要转换成模式的数据。 输入数据是由某些东西分隔的列表(因为它很容易找到和替换)例如。逗号

food,apple,10,10 
sweets,candy,20,20

我想把它转换成XML:

<Item>
    <Product type="food" name="apple" price"10" quantity="10">
</Item>
<Item>
    <Product type="sweets" name="candy" price"20" quantity="20">
</Item>

你需要一个正则表达式 find/replace:

使用查找对话框,替换标签:

  1. 查找内容: ^([^,]*),([^,]*),([^,]*),([^\r\n]*)(\R)*
  2. 替换为: <Item> <Product type="" name="" price="" quantity=""> </Item>
  3. 勾选左下正则表达式
  4. 全部替换

解释:

  • 查找将字符串拼接成逗号分隔的部分并捕获 </code> 到 <code>
  • 中的值
  • 捕获换行符
  • 替换将捕获的值放入 XML-Node

替换:(\w+),(\w+),(\w+),(\w+)

与 : <Item>\n <Product type="" name="" price="" quantity="">\n</Item>

Please check out this demo.