Match character placeholders (places where an input cursor can be putted)

若如初见. 提交于 2020-01-25 07:22:12

问题


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.

Current input

sentence

Desired input

| Sentence |

or:

| Sentence. Sentence |

and so on...


How I thought to do it

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 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.

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|) .

My question

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:

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


回答1:


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 $.

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|



来源:https://stackoverflow.com/questions/59188340/match-character-placeholders-places-where-an-input-cursor-can-be-putted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!