正则表达式匹配字符串模式中的某些字符

时间:2022-08-22 11:54:29

I have a example of many of string pattern like this but I want to show some example.

我有一个像这样的字符串模式的例子,但我想展示一些例子。

from:   [name:  Illianney Amada
id:     674176087]
from:   [name:  Natalia Morel-Gibbs
id:     100003799207624]
from:   [name:  Jules Kaneyge Pand
id:     100000110811550]

And, I would like to illustrate the parameter type like this: (Watch String and SequenceOfNumber)

而且,我想说明这样的参数类型:( Watch String和SequenceOfNumber)

from:   [name:  String
id:     SequenceOfNumber]

but actually, it was represented from this

但实际上,它是由此表示的

from:\t[name:\tString\nid:\tSequenceofNumber]

So, I would like to replace the "\n" that is between "String" and "id:" with ",\t" or tab character. The result should be like this

所以,我想用“,\ t”或制表符替换“String”和“id:”之间的“\ n”。结果应该是这样的

from:\t[name:\tString,\tid:\tSequenceofNumber]\n
from:\t[name:\tString,\tid:\tSequenceofNumber]\n
from:\t[name:\tString,\tid:\tSequenceofNumber]\n

Or in other way like this

或者像这样的其他方式

from:   [name:  String,    id:     SequenceOfNumber]
from:   [name:  String,    id:     SequenceOfNumber]
from:   [name:  String,    id:     SequenceOfNumber]

Note: I implement the regex replacing with Python module re

注意:我用Python模块重新实现正则表达式替换

2 个解决方案

#1


1  

Update:

import re
fixed = re.sub(r"(\[name:.*?)\n", r"\1,\t", originalString, re.M)

Results in:

from:   [name:  Illianney Amada,       id:     674176087]
from:   [name:  Natalia Morel-Gibbs,   id:     100003799207624]
from:   [name:  Jules Kaneyge Pand,    id:     100000110811550]

Working example: http://regex101.com/r/wN7aT0

工作示例:http://regex101.com/r/wN7aT0


Old:

If you've only got the one \n there, you could do:

如果你只有那个人,你可以这样做:

originalString = "from:\t[name:\tString\nid:\tSequenceofNumber]"
fixedString = ",\t".join(originalString.split("\n"))

This will split the string on \n and join it back together with ,\t, resulting in:

这将在\ n上拆分字符串并将其与\ t \ t一起重新连接,从而产生:

from:\t[name:\tString,\tid:\tSequenceofNumber]

Caveat: in your original example, you didn't actually set a string in the variable. Are you perhaps opening this from a text file? If so, that changes the answer dramatically, because you might be looping one line at a time.

警告:在您的原始示例中,您实际上并未在变量中设置字符串。你是否可以从文本文件中打开它?如果是这样,那么会大大改变答案,因为您可能一次循环一行。

#2


0  

If only replace '\n' in pattern, then like this:

如果只替换模式中的'\ n',那么像这样:

import re

data = """from:   [name:  Jules Kaneyge Pand
id:     100000110811550]
from:   [name:  abcd
id:     100000110811550]
           """

print ',\tid:'.join(re.split("[^\]]\s*\n\s*id:", data))

You will get:

你会得到:

from:   [name:  Jules Kaneyge Pan,  id:     100000110811550]
from:   [name:  abc,    id:     100000110811550]

#1


1  

Update:

import re
fixed = re.sub(r"(\[name:.*?)\n", r"\1,\t", originalString, re.M)

Results in:

from:   [name:  Illianney Amada,       id:     674176087]
from:   [name:  Natalia Morel-Gibbs,   id:     100003799207624]
from:   [name:  Jules Kaneyge Pand,    id:     100000110811550]

Working example: http://regex101.com/r/wN7aT0

工作示例:http://regex101.com/r/wN7aT0


Old:

If you've only got the one \n there, you could do:

如果你只有那个人,你可以这样做:

originalString = "from:\t[name:\tString\nid:\tSequenceofNumber]"
fixedString = ",\t".join(originalString.split("\n"))

This will split the string on \n and join it back together with ,\t, resulting in:

这将在\ n上拆分字符串并将其与\ t \ t一起重新连接,从而产生:

from:\t[name:\tString,\tid:\tSequenceofNumber]

Caveat: in your original example, you didn't actually set a string in the variable. Are you perhaps opening this from a text file? If so, that changes the answer dramatically, because you might be looping one line at a time.

警告:在您的原始示例中,您实际上并未在变量中设置字符串。你是否可以从文本文件中打开它?如果是这样,那么会大大改变答案,因为您可能一次循环一行。

#2


0  

If only replace '\n' in pattern, then like this:

如果只替换模式中的'\ n',那么像这样:

import re

data = """from:   [name:  Jules Kaneyge Pand
id:     100000110811550]
from:   [name:  abcd
id:     100000110811550]
           """

print ',\tid:'.join(re.split("[^\]]\s*\n\s*id:", data))

You will get:

你会得到:

from:   [name:  Jules Kaneyge Pan,  id:     100000110811550]
from:   [name:  abc,    id:     100000110811550]