匹配字符占位符(可以放置输入光标的位置) [英] Match character placeholders (places where an input cursor can be putted)

查看:170
本文介绍了匹配字符占位符(可以放置输入光标的位置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual Studio Code,并且有1000行长的 .md 文档,其中的每一行通常包含一个或多个句子。

我想用竖杠(每个从左向一个右边,并带有相应的空白),用于将一长串句子转换为一个(单列)减价表。

I work with Visual Studio Code and I have a problem with a 1,000 lines long .md document in which generally each line contains one or more sentence.
I desire to wrap each sentence with vertical bars (one from the left and one from the right, with respective empty spaces), for the process of transforming the long list of sentences into a (single columned) markdown table.

当前输入

sentence

所需输入

| Sentence |

或:

| Sentence. Sentence |

依此类推...

通常,我可以将输入光标放在( l光束光标)在文本字段中字符旁边的任何位置;

我认为可以将位置(我可以在其中放置输入光标的位置)命名为字符占位符( CP )。

In general, I can put my input cursor (l-beam cursor) anywhere beside characters in a text field;
I assume that any such "place" (where I can put my input cursor), is plausible to be named a "Character Placeholder" (CP).

I假定按字符创建CP(例如,只有一个字符的行将包含两个CP),如果这样,则可以在该字符之前和之后自由匹配CP1和CP2(或CP0和CP1-取决于基索引)

I assume that CPs are created per characters (for example, a line with only one character would contain two CPs) and if so, one could freely match CP1 and CP2 (or CP0 and CP1 - depends on base index), before and after that character respectively.

我想命令VSCODE添加竖线和相应的空白区域( | U + 0020 )在每行第一个字符之前的CP中,以及在每行最后一个字符之后的CP中( U + 0020 | )。

I would like to command VSCODE to add a vertical bar and a respective empty space (|U+0020) in the CP available before the first character in every line, as well as in the CP available after the last character in every line (U+0020|) .

我只知道匹配字符的方法(或字符集)本身,使用正则表达式,但我不知道如何仅匹配CP,我问:

As I only know ways to match characters (or sets of characters) themselves, with regex, but I don't know how to match CPs only, I ask:

如果有的话,怎么能匹配CP?

How could one match CPs if at all, with current technology, so to command a program to add data X in CP Y?

推荐答案

使用正则表达式很简单。正则表达式具有字符串的开始和结尾的标识符。 (根据您的输入,您可以将每个句子视为自己的字符串。)

This is simple to do with regex. regex has identifiers for 'start of' and 'end of' strings. (depending on your input you can treat each sentence as its own string).

要匹配字符串的开头,正则表达式为- ^ 而匹配字符串末尾的正则表达式为 $

To match start of strings the regex is - ^ while to match the end of strings the regex is $.

现在要实现您的请求,您需要做的就是使用-
^(。*?)$ 并替换为 | \s $ 1\s | $ 1 是一个返回引用被捕获的组)看起来像-搜索^(。*)$替换| \s $ 1\s |

Now to implement your request all you need to do is match the whole line using - ^(.*?)$ and replace it with |\s$1\s| (the $1 is a back reference to the captured group) It would look something like - Search ^(.*)$ Replace |\s$1\s|

这篇关于匹配字符占位符(可以放置输入光标的位置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆