简体   繁体   English

如何将正则表达式导出为CSV,并确保每个分组都进入正确的列?

[英]How do I export regex to CSV and ensure that each grouping goes into the correct column?

I've created the regular expression to group the data I need to collect. 我创建了正则表达式来对需要收集的数据进行分组。 However, I'm really lost as to how I go about exporting this. 但是,我真的迷失了如何导出它。

Here's what I've done https://regex101.com/r/C7KvkH/5 这是我所做的https://regex101.com/r/C7KvkH/5

What I need to do next is to have 4 columns in a spreadsheet, such as LibreOffice Spreadsheet or Google Sheets. 我接下来要做的是在电子表格中有4列,例如LibreOffice Spreadsheet或Google Sheets。 So I need the data to correspond to the following columns: 因此,我需要数据对应以下几列:

'frequency' group 1 -first column, 'word' group 2 -second column, 'gender' group 3 -third column, 'sentence' group 4 -fourth column “频率”组1-第一列,“单词”组2-第二列,“性别”组3-第三列,“句子”组4-第四列

Do I save this data to a text file and then change it to a CSV file? 我是否将此数据保存到文本文件,然后将其更改为CSV文件?

Do I need to create tabs? 我需要创建标签吗?

How do I go about doing that? 我该怎么做?

I have expanded on the pattern that I provided for your second question / sample of text (French). 我已经扩展了为您的第二个问题/文本样本(法语)提供的模式。

This pattern works with both of your sample texts (French and Spanish). 此模式适用于您的两个示例文本(法语和西班牙语)。 It provides five capture groups as per your second question. 根据您的第二个问题,它提供了五个捕获组。

/^(\d+) +(\w+(?:, ?\w+)*) +([acdefijlmnoprtuv()]+(?:, ?[acdefijlmnoprtuv()]+)*) +([\S\s]+?)\n\x{2022} +([\S\s]+?)\n\d+ \| [-+\dno ]+\s*/um

Demo Link 演示链接

See my remarks at the other answer . 参见我在其他答案上的评论

Adjustments to my other pattern: 调整我的其他模式:

  • Expanded the second capture group to allow comma separated values. 扩展了第二个捕获组,以允许用逗号分隔值。 eg el, la 例如, el, la
  • Added u to the character classes of capture group 3. (for #56 & #72) u添加到捕获组3的字符类中。(对于#56和#72)
  • Expanded last character class to [-+\\dno ] for occurrences like text block #8. 将最后一个字符类扩展到[-+\\dno ][-+\\dno ]诸如文本块#8的出现。

After using this pattern with preg_replace() to perform the desired tabbing, you can use php's menu of csv functions to produce the file that you need. 在将此模式与preg_replace()以执行所需的制表符之后,可以使用php的csv函数菜单来生成所需的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM