正则表达式:如何在XML属性值周围放置单引号?

时间:2021-04-25 21:47:03

i have a string that looks like

我有一个看起来像的字符串

"<input id=a/>"<input id=b/>"<input id=c/>etc.

I need to change it to

我需要改成它

"<input id='a'/>"<input id='b'/>"<input id='c'/>etc,

any ideas how?

任何想法怎么样?

3 个解决方案

#1


4  

In C# you could write it as:

在C#中你可以把它写成:

resultString = Regex.Replace(subjectString, @"(<.*?id\s*=\s*)(\w+)(.*?>)", "$1'$2'$3", RegexOptions.Multiline);

In VB.Net it would simply be:

在VB.Net中它只是:

ResultString = Regex.Replace(SubjectString, "(<.*?id\s*=\s*)(\w+)(.*?>)", "$1'$2'$3", RegexOptions.Multiline)

In canonical Perl, you could write it as:

在规范的Perl中,您可以将其写为:

$subject =~ s/(<.*?id\s*=\s*)(\w+)(.*?>)/$1'$2'$3/mg;

In PHP:

$result = preg_replace('/(<.*?id\s*=\s*)(\w+)(.*?>)/m', '$1\'$2\'$3', $subject);

In Java:

resultString = subjectString.replaceAll("(?m)(<.*?id\\s*=\\s*)(\\w+)(.*?>)", "$1'$2'$3");

In Javascript:

result = subject.replace(/(<.*?id\s*=\s*)(\w+)(.*?>)/mg, "$1'$2'$3");

#2


1  

How about this:

这个怎么样:

%s/"<input id=\(.\)\/>/"<input id='\1'\/>/g

This would also work:

这也有效:

%s/\("<input id=\)\(.\)\/>/\1'\2'\/>/g

#3


0  

It's hard to really answer this with just one small sample. For the given sample text, you can search for the regex:

只用一个小样本就很难真正回答这个问题。对于给定的示例文本,您可以搜索正则表达式:

=(\w)

and replace it with:

并替换为:

='$1'

or:

='\1'

Depending on whether the programming language you're working with interprets $1 or \1 as a reference to the first capturing group in the replacement text.

取决于您使用的编程语言是否将$ 1或\ 1解释为替换文本中第一个捕获组的引用。

This trivial search-and-replace works perfectly on your given sample text.

这种简单的搜索和替换可以完美地处理您给定的示例文本。

I fear that it won't work on your actual data. If that's the case, then that's because your sample text isn't representative of your actual data. The hardest part in creating regular expressions is to figure out what you want to match, and what you don't want to match. This is something you have to specify in your question.

我担心它对你的实际数据不起作用。如果是这种情况,那是因为您的示例文本不能代表您的实际数据。创建正则表达式最困难的部分是弄清楚要匹配的内容以及您不想匹配的内容。这是您必须在问题中指定的内容。

#1


4  

In C# you could write it as:

在C#中你可以把它写成:

resultString = Regex.Replace(subjectString, @"(<.*?id\s*=\s*)(\w+)(.*?>)", "$1'$2'$3", RegexOptions.Multiline);

In VB.Net it would simply be:

在VB.Net中它只是:

ResultString = Regex.Replace(SubjectString, "(<.*?id\s*=\s*)(\w+)(.*?>)", "$1'$2'$3", RegexOptions.Multiline)

In canonical Perl, you could write it as:

在规范的Perl中,您可以将其写为:

$subject =~ s/(<.*?id\s*=\s*)(\w+)(.*?>)/$1'$2'$3/mg;

In PHP:

$result = preg_replace('/(<.*?id\s*=\s*)(\w+)(.*?>)/m', '$1\'$2\'$3', $subject);

In Java:

resultString = subjectString.replaceAll("(?m)(<.*?id\\s*=\\s*)(\\w+)(.*?>)", "$1'$2'$3");

In Javascript:

result = subject.replace(/(<.*?id\s*=\s*)(\w+)(.*?>)/mg, "$1'$2'$3");

#2


1  

How about this:

这个怎么样:

%s/"<input id=\(.\)\/>/"<input id='\1'\/>/g

This would also work:

这也有效:

%s/\("<input id=\)\(.\)\/>/\1'\2'\/>/g

#3


0  

It's hard to really answer this with just one small sample. For the given sample text, you can search for the regex:

只用一个小样本就很难真正回答这个问题。对于给定的示例文本,您可以搜索正则表达式:

=(\w)

and replace it with:

并替换为:

='$1'

or:

='\1'

Depending on whether the programming language you're working with interprets $1 or \1 as a reference to the first capturing group in the replacement text.

取决于您使用的编程语言是否将$ 1或\ 1解释为替换文本中第一个捕获组的引用。

This trivial search-and-replace works perfectly on your given sample text.

这种简单的搜索和替换可以完美地处理您给定的示例文本。

I fear that it won't work on your actual data. If that's the case, then that's because your sample text isn't representative of your actual data. The hardest part in creating regular expressions is to figure out what you want to match, and what you don't want to match. This is something you have to specify in your question.

我担心它对你的实际数据不起作用。如果是这种情况,那是因为您的示例文本不能代表您的实际数据。创建正则表达式最困难的部分是弄清楚要匹配的内容以及您不想匹配的内容。这是您必须在问题中指定的内容。