Emeditor提取每行显示的字符串(保持行顺序)

huangapple go评论132阅读模式
英文:

Emeditor to extract displayed strings for each line (maintaining the line order)

问题

如何在EmEditor中使用正则表达式查找和提取字符串,但保留相同行数的字符串加上分隔符?

当我尝试提取显示的字符串时,我得到的输出是,将每个匹配的字符串提取到新的一行。但我的目标是从每一行提取这些匹配项(删除我不想要的值)。

例如,

从以下开始:

dog cat food
prince dog food

我想要得到:

dog food
prince food

或者

带有分隔符

dog, food
prince, food

但是使用EmEditor时

  1. 按Ctrl + F
  2. 输入(\b\w+\b)$|^\w+,然后选择正则表达式并提取>仅显示匹配的字符串

输出是

dog
food
prince
food

是否可以使用EmEditor或通过宏来实现这一目标?

英文:

How can I use emeditor to find and extract regex strings but maintain the same lines +/- a delimiter?

The output I get when I try to extract displayed strings, extracts each matched string to a new line. But my goal is to extract these matches from each line (removing the values I dont want)

For example

Starting with:

dog cat food
prince dog food

I would like to end up with

dog food
prince food

Or

with a delimiter

dog, food
prince, food

But using Emeditor

  1. Cntr+F
  2. (\b\w+\b)$|^\w+ and then selecting Regular expressions and extract>display matched strings only

the ouput is

dog
food
prince
food

Can this be accomplished using EmEditor or through a macro?

答案1

得分: 1

I hope I understand the task correctly, the first and third of three values should remain.

解决方案根据您的示例:
结果在新文档中输出。

替换对话框

  • 搜索项类似于您的尝试
    ^(\b\w+\b) \b\w+\b (\b\w+\b)$
  • 替换为
    \1 \2
    (在这种情况下,分隔符是空格,您可以在\1和\2之间放逗号或其他喜欢的字符)
  • 提取(按钮)

请检查是否在“高级”中有设置阻止所需结果,否则请按重置。
请使用最新版本的 EmEditor。

新文档中的结果:

解决方案方法2:
从三个值中删除中间的一个。
在上述对话框中,单击“全部替换”而不是提取函数。如果您不想更改原始文档,请使用副本。

英文:

I hope I understand the task correctly, the first and third of three values should remain.

Solution approach according to your example:
The result is output in a new document.

Replace Dialog

  • The search term is analogous to your attempt

    ^(\b\w+\b) \b\w+\b (\b\w+\b)$

  • Replace with:

    \1 \2
    (delimiter is space in this case, put comma or what you like between \1 and \2)

  • Extract (Button)

Please check if a setting in "Advanced" prevents the desired result, otherwise press reset.
Please use the latest version of EmEditor.

Emeditor提取每行显示的字符串(保持行顺序)

Result in the new document:

Emeditor提取每行显示的字符串(保持行顺序)

Solution approach 2:
From three values the middle one is deleted.
In the same dialog as above, click on "Replace All" instead of the Extract function. If you do not want to change the original document, please use a copy.

答案2

得分: 1

使用筛选工具栏而不是查找对话框。

  1. 筛选工具栏中,点击使用正则表达式按钮,并在筛选下拉列表框中输入一个正则表达式,例如 ^\w+|\w+$

  2. 点击筛选工具栏中的提取全部按钮,然后在弹出菜单中选择提取选项

  3. 筛选提取选项对话框中,选择提取所有匹配的字符串,并输入 \t, 作为分隔符。点击确定。

  4. 再次点击筛选工具栏中的提取全部按钮,然后在弹出菜单中选择提取匹配的字符串

如果你将这个过程录制成宏,你将得到如下的宏:

document.Filter("^\\w+|\\w+$",0,eeFindReplaceRegExp,0,0,0,0,0);
editor.ExecuteCommandByID(4084);  // 提取匹配的字符串

如果你需要对文件夹中的多个文件运行此宏,请参见:
https://stackoverflow.com/questions/70576096/emeditor-run-a-macro-for-all-file-inside-a-folder/70581899?noredirect=1#comment134640574_70581899

英文:

Use the Filter toolbar instead of the Find dialog.

  1. In the Filter toolbar, click the Use Regular Expressions button, and enter a regular expression, for instance, ^\w+|\w+$, in the Filter drop-down list box.

Emeditor提取每行显示的字符串(保持行顺序)

  1. Click the Extract All button in the Filter toolbar, then select Extract Options in the popup menu.

Emeditor提取每行显示的字符串(保持行顺序)

  1. In the Filter Extract Options dialog box, select Extract all matched strings, and enter \t or , as a Delimiter. Click OK.

  2. Click the Extract All button again in the Filter toolbar, then select Extract Matched Strings in the popup menu.

If you record this procedure to a macro, you will get a macro like this:

document.Filter("^\\w+|\\w+$",0,eeFindReplaceRegExp,0,0,0,0,0);
editor.ExecuteCommandByID(4084);  // Extract Matched Strings

If you need to run this macro against many files in a folder, please see:
https://stackoverflow.com/questions/70576096/emeditor-run-a-macro-for-all-file-inside-a-folder/70581899?noredirect=1#comment134640574_70581899

答案3

得分: 0

EmEditor具有其他文本编辑器不具备的强大功能,但很少提及,那就是**\J模式**(在替换表达式中使用JavaScript函数或方法),可以弥补正则表达式在某些情况下的不足。例如,可以使用以下表达式处理此问题。

查找:^.+$

替换:\J ""\0".replace(/ cat | dog /g,"",")

˽cat˽˽dog˽ 是要替换的关键词,您可以根据需要进行更改。
单击“全部替换”按钮后,所需的字符串将保留在同一行中。

另一个链接的屏幕截图

英文:

EmEditor has a powerful feature that other text editors do not have, but it is rarely mentioned, which is the \J mode (using JavaScript function or methods in replacement expressions), which can compensate for the shortcomings of regular expressions in certain situations. For example, this question can be handled using the following expression.

Find:^.+$

Replace: \J "\0".replace(/ cat | dog /g,",")

˽cat˽ and ˽dog˽ are the keywords to be replaced and you can change them according to your requirements.
After clicking Replace All button , wanted strings will be left in same line.

screenshot from another link

huangapple
  • 本文由 发表于 2023年5月30日 10:34:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76361288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定