English 中文(简体)
如何在案文之间使用定期表述来插入案文?
原标题:How to use Regular Expression to insert text in between text?

我有一个独特的情景。 有一个网络应用程序,是一个模拟器,用以检查XML中的数据发送情况,使数据回归xml,并核实Xml中的一些细节。

现在,我发送的xml数据有许多细节。 在这一xml中,我必须插入我在测试中界定的参数。 我无法得到,在发送之前如何将数据作为Xml的参数发送。

......

id= 12345 ><version>1.3.4<</version><accno>1234567890</accno>add<address details</> ..........

现在,在这种xml结构中,我已经实现了以下参数:<代码><accno>1234567890</accno>。 页: 1

现在,我想在xml中将六氯苯作为参数,而不是在xml的硬编码价值。 请建议如何做到这一点。

最佳回答

我要说的是编辑Xml,reg是坏的。

只是回答直接提问使用等子。 例如。

str.gsub(/reg_match/, newstring)

但这更好的办法是使用hpricot

或者你也可以使用废墟模板。

require  erb 
require  ostruct 

data = {:accno => "1234567890"}
variables = OpenStruct.new(data)

template = "<id= 12345 ><version>1.3.4</version><accno><%= accno%></accno>"

res = ERB.new(template).result(variables.instance_eval { binding })
puts res
问题回答

XML不是常规的,而是无环境的。 使用Nokogiri等适当的教区,而不是reg。 见。 RegEx对开标进行对等,但XVD自封标记除外。

As answer, as requested.

首先确定模式,然后用 g子取而代之。

xml_data.gsub! (pattern, replacement)

http://ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.html#String.gsub_oh

这样做的快道是用sub子(如Rajkaran所说)。 http://www.germane-software.com/software/rexml/“rel=“nofollow” 或其他一些Xml图书馆。 投资应当与你今后如何使用此类物品有关。





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签