在一行后面添加字符

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

How to add character after a line

问题

我试图执行一些正则表达式步骤,我想在这些行的末尾添加引号和逗号 (",),而不改变行中的任何其他字符。如何保持原样但在单词device1device2device3后添加",

我正在处理的行的示例:

object network device1
host 192.168.1.11
object network device2
host 192.168.1.12
object network device 3
host 192.168.1.13

经过我第一步的正则表达式处理后,我已经修改了第一行,包括花括号和一些格式,如下所示,带有单词"category"和"name"。然而,我不想更改单词device1,但想在单词device1后包括引号和逗号:

{
"category": "network",
"name": "device1
host 192.168.1.11
{
"category": "network",
"name": "device2
host 192.168.1.11
{
"category": "network",
"name": "device3
host 192.168.1.13

我无法在我的正则表达式替换序列的第一步中包括"和`,``?我在使用 regexr.com 和 Notepad++。

英文:

I'm trying to perform a few regex steps, and I'd like to add a quotation mark and a comma (",) at the end of these lines without altering any of the rest of the characters in the line.

How would I keep things intact but add the ", after the words: device1, device2, device3 ?

Example of lines I'm working with:

object network device1  
host 192.168.1.11  
object network device2  
host 192.168.1.12  
object network device 3  
host 192.168.1.13  

After my first step of regex, I have modified my first line to include the curly bracket and some formatting with the words "category" and "name" as shown below. However, I don't want to change the word device1, but want to include a quotation and comma after the word device1

{  
"category": "network",  
"name": "device1  
host 192.168.1.11  
{  
"category": "network",  
"name": "device2  
host 192.168.1.11  
{  
"category": "network",  
"name": "device3  
host 192.168.1.13  

I can't figure out how to include the ", with my first step in my regex replace sequence?

I'm using both regexr.com and Notepad++.

答案1

得分: 1

你可以使用这个正则表达式来匹配输入数据中的每个实体:

object\s+(\w+)\s+([^\r\n]+)[\r\n]+host\s+([\d.]+)

这匹配了:

  • object\s+:单词"object"后面跟着一些空格
  • (\w+):一些字母数字字符和下划线(_)字符,捕获在组1中
  • \s+:一些空格
  • ([^\r\n]+):一些非行尾字符,捕获在组2中
  • [\r\n]+:一些行尾字符
  • host\s+:单词"host"后面跟着一些空格
  • ([\d.]+):一些数字和点号字符,捕获在组3中

然后可以替换为:

{
 "category": "$1",
 "name": "$2",
 "host": "$3"
},

以便输出(对于您的示例数据)如下:

{
 "category": "network",
 "name": "device1",
 "host": "192.168.1.11"
},
{
 "category": "network",
 "name": "device2",
 "host": "192.168.1.12"
},
{
 "category": "network",
 "name": "device 3",
 "host": "192.168.1.13"
},

现在,您可以简单地在文件开头添加[,并将最后一个,替换为],以创建一个有效的JSON文件。

英文:

You can use this regex to match each entity in your input data:

object\s+(\w+)\s+([^\r\n]+)[\r\n]+host\s+([\d.]+)

This matches:

  • object\s+ : the word "object" followed by a number of spaces
  • (\w+) : some number of word (alphanumeric plus _) characters, captured in group 1
  • \s+ : a number of spaces
  • ([^\r\n]+) : some number of non-end-of-line characters, captured in group 2
  • [\r\n]+ : some number of end-of-line characters
  • host\s+ : the word "host" followed by a number of spaces
  • ([\d.]+) : some number of digit and period characters, captured in group 3

This can then be replaced by:

{\n "category": "$1",\n "name": "$2",\n "host": "$3"\n},

To give output (for your sample data) of:

{
 "category": "network",
 "name": "device1",
 "host": "192.168.1.11"
},
{
 "category": "network",
 "name": "device2",
 "host": "192.168.1.12"
},
{
 "category": "network",
 "name": "device 3",
 "host": "192.168.1.13"
},

Regex demo on regex101

Now you can simply add [ at the beginning of the file and replace the last , with a ] to make a valid JSON file.

答案2

得分: 0

这是正则表达式 "name": "(device\d+) ,但由于您没有提到任何编程语言,根据您使用的语言,可能会出现一些模式错误,例如在 Java 中需要转义字符,所以如果您在使用 Java,则可以使用以下正则表达式
\"name\": \"(device\d+)
现在您需要提取组 (device\d+) 并将您的 " 放在那里
例如,在 Java 中,您可以使用 string.replaceAll 来实现它。

英文:

This is the regex "name": "(device\d+) but since you have not mentioned any programming language you might get some pattern error based on the language you are using for example "" in java will need escape character so if you are using java then use this regex
\"name\": \"(device\d+)
Now you have to extract group (device\d+) and put your " there
for example in java you can do it with string.replaceAll

huangapple
  • 本文由 发表于 2023年2月19日 11:36:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497826.html
匿名

发表评论

匿名网友

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

确定