关于 Vim 编辑器

Problem Description
Vim is a text editor which developed from vi. Due to its powerful function in code complete, compile, and error jump, it’s widely used by programmers. The same as Emacs, it’s the most popular text editor among users of UNIX. As such an excellent text editor, Vim has various of orders. Now, we’re asking you to write a program that simulates Vim’s replace order.

The format of Vim’s replace order is ([] means optional, {} means necessary) :

:[range]s/{pattern}/{string}/[flag]

In the order above,

‘:’ means the start of a replace order,

[range] indicates the range of the order, that is, the order works in which lines.

‘s’ is short for substitute.

{pattern} and {string} represent the string to match and replace to, respectively.

‘/’ is used to mark the beginning and ending of {pattern} and {string}.

{flag} is used to open or close some options.

{range} is often two integers separated by a comma, indicating the start line and end line’s line number.

For example, “4,8” means from line 4 to line 8 (including line 4, line 8). Line number starts from 1. You can also use a ‘%’ to represent all lines.

(Additional, Vim provides many more flexible formats. Such as, bypassing a number means the line cursor stays, ‘′meansthelastlineofthetext.So,“,” means from the line cursor stays to the last line. )

{pattern} and {string} both support regular expression (if you haven’t ever heard of it, go to Google for help). If {pattern} is empty, the {pattern} of the last replace order will be used.

Obviously, ‘/’ can’t be included in {pattern} and {string}. So, an additional escape character ‘\’ is used. For example, if you want to replace “
” to “
”, you cannot write:

:%s/
/
/g

Instead, you should write:

:%s/
/
/g

If there’re too many ‘/’s in the expression, (for instance, "file:///usr/share/man/man1/vim.1.gz"), it will become troublesome. So, people think of a solution, that is, use another character as the separator (the first character after ‘s’ is always treated as the separator). For example, when using ‘+’ as the separator, the order above can be written this way:

:%s+
+
+g

There’re many kinds of [flag]. ‘g’ means replace every time it matches. Without a ‘g’, it will only replace the first matching string. For example:

#include

Execute the order bellow:

:%s/i//g

Result is:

#nclude

While executing this order bellow:

:%s/i//

Result is:

#nclude

Other flags including: ‘c’ indicates a confirm is required before every replacement, ‘i’ indicates case insensitive.

Here comes a problem, what if you want to replace “a” to “aa”? Somebody may doubt that it will go to an endless loop, but in fact, it won’t. Because there is a rule that, in a replacement, the replaced characters can’t be replaced again. So, if you want to replace “a” to “aa”, it’s in fact that every successive “a” string is doubled in length.

To simplify the problem, we make some appointments:

  1. [range] must appear, in the form of “%” or “a,b” (a, b are both integers, and a<=b)

  2. {pattern} and {string} are both consist of characters, numbers, spaces and “_” (not including any separator below, so an escape character is not needed)

  3. You can choose one of these characters as the separator: /~!@#$%^&*()-+=

  4. [flag] is always a “g”

A big example:

Original text:

If the Tao is greet, then the operating system is greet.

If the operating system is greeter, then the compiler is greet.

If the compiler is greeter, then the applications is greet.

The user is pleased and there is harmony in the world.

Replace order:

:[1,4]s/greet/great/g

Or:

:%s+greet+great+g

Either will replace the original text to:

If the Tao is great, then the operating system is great.

If the operating system is greater, then the compiler is great.

If the compiler is greater, then the applications is great.

The user is pleased and there is harmony in the world.

Please write a program to simulate this simplified vim replace order.

Input
The input will consist of one case.

The first line will be a positive integer L (L <= 100), specifying the number of lines to be processed. Then L lines of text are given. Each line has no more than 100 characters.

After that, several pieces (<= 50) of replace orders are given (one per line). It is ensured that any line of text will never have more than 100 characters during the replacement.

Output
After the execution of every replace order, output the line number and content of the lines that have been replaced, order by line number from small to big. In each line, first output the line number, which has a width of 4 characters, right-aligned, then 2 spaces, then the text after replacement.

If any order replaced nothing, output "Pattern not found".

Output a blank line between any two replace order’s result.

Sample Input
4
If the Tao is greet, then the operating system is greet.
If the operating system is greeter, then the compiler is greet.
If the compiler is greeter, then the applications is greet.
The user is pleased and there is harmony in the world.
:1,3s/greet/great/g
:%s//great/g

Sample Output
1 If the Tao is great, then the operating system is great.
2 If the operating system is greater, then the compiler is great.
3 If the compiler is greater, then the applications is great.

Pattern not found

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问

相似问题

3
Win10下Vim内调试C++程序,弹出cmd窗口却无法滑动vim内窗口,无法看代码
1
无网win10如何用vim打造python编辑器
0
为什么vim里面设置的leader键不起作用?
1
Ubuntu16.04虚拟机上安装vimplus插件后出现的问题
0
请问用vim对php进行编程,如何能让vim实时提示当前php语法错误?
0
Deepin系统中,安装了vim-instant-Markdown之后新建一个markdown类型文件会出错
0
VIM-adventures,用C语言来实现
1
vim的边框有大于号,是ctag没有找到定义吗?还是什么原因?怎么去除?强迫症表示不能忍
1
code blocks可以编译但是到Linux里的vim时无法编译成功是怎么回事?
1
ubuntu 4.15.0-54-generic vi命令为什么指向vim
2
Linux如何用一行vi命令完成编辑文档的一系列操作?
0
vim中映射的组合键的按不出效果,应该怎么个按法?
1
linux vim /etc/profile 添加java环境变量后 可能强制退出 后每次登录都会出非正常界面 怎么弄
1
Oracle VM VirtualBox 进行 vim /usr/local/nginx/conf/nginx.conf 配置以后怎么保存退出到如图状态 萌新求助!!!
0
linux下pandoc使用自定义的template模板将md文件转换为中文pdf文件-报错问题
0
VMware启动openwrt失败!!!请教各位大神!!附图!!!
0
Linux系统中的音乐播放器
2
关于Docker虚拟机中安装使用vi/vim命令及apt-get update命令的疑问
1
awk怎么提取匹配数组元素的行
0
VIM-adventures 编辑器问题