通用正则表达式代码 [英] universal regex code

查看:20
本文介绍了通用正则表达式代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是网络开发的初学者,尤其是正则表达式代码,想知道是否有可能编写一个通用代码来检索站点的坐标,而不管它们是在 iframe 代码或图像 URL 中还是在

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=key" type="text/javascript"></script><script type="text/javascript">//<![CDATA[var html = '';无功纬度 = 44.799439;var lang = 20.483494;如果 (GBrowserIsCompatible()){...

第二个例子:

<iframe frameborder="0" border="0" scrolling="no" marginwidth="0" marginheight="0" title=""src="http://www.kolektiva.rs/beograd/dailydeal/vendor/map/center/44.79848,20.473856/"></iframe>

是否可以先删除所有字符和所有仅是小数点坐标的字符,然后再取?

解决方案

第一个例子:

评估以下 XPath 1.0 表达式:

normalize-space(translate(.,translate(.,' 0123456789.', ''), ' '))

生产:

44.799439 20.483494

这可以通过任何 XPath 引擎完成,并且上下文节点必须是文档节点,或者在 XSLT 1.0 中,如下:

<xsl:output omit-xml-declaration="yes" indent="yes"/><xsl:strip-space elements="*"/><xsl:template match="/"><xsl:value-of select=标准化空间(翻译(.,翻译(.,'0123456789.',''),''))"/></xsl:模板></xsl:stylesheet>

应用于此 XML 文档时(与提供的文档相同,但已完成):

<div id="g_map"></div><script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=key" type="text/javascript"></script><script type="text/javascript">//<![CDATA[var html = '';无功纬度 = 44.799439;var lang = 20.483494;如果 (GBrowserIsCompatible()){}]]></html>

结果是:

44.799439 20.483494

第二个例子:

<xsl:output method="text"/><xsl:template match="/"><xsl:for-each select="///*/@src"><xsl:value-of select=标准化空间(翻译(.,翻译(.,'0123456789.',''),''))"/></xsl:for-each></xsl:模板></xsl:stylesheet>

生产:

..44.7984820.473856

需要多一点清洁.

<小时>

二.更严重的处理(XPath 2.0/XSLT 2.0)

这种转换确实是通用的"并且适用于两个示例:

<xsl:output omit-xml-declaration="yes" indent="yes"/><xsl:strip-space elements="*"/><xsl:template match="/"><xsl:analyze-string select="string-join((.,///*/@*), ' ')"正则表达式="\-?\d+\.\d+"><xsl:匹配子串><xsl:sequence select="concat(., ' ')"/></xsl:matching-substring></xsl:analyze-string></xsl:模板></xsl:stylesheet>

应用于第二个 XML 时:

<iframe frameborder="0" border="0" scrolling="no" marginwidth="0"marginheight="0" 标题=""src="http://www.kolektiva.rs/beograd/dailydeal/vendor/map/center/44.79848,20.473856/"></iframe>

产生想要的、正确的结果:

44.79848 20.473856

当应用于第一个示例的 XML 时,它也会产生想要的结果:

44.799439 20.483494

I am a beginner in web development especially in the regex code and want to know if it is possible to write a universal code that will retrieve the coordinates of the sites regardless of whether they are in the iframe code or image URL or in javascript.

<div id="g_map"></div>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=key" type="text/javascript"></script>
<script type="text/javascript"> 

//<![CDATA[

var html = '';    
var lat  = 44.799439;
var lang = 20.483494;
if (GBrowserIsCompatible())
{
...

second example:

<div class="map">
    <iframe frameborder="0" border="0" scrolling="no"  marginwidth="0" marginheight="0" title=""  
          src="http://www.kolektiva.rs/beograd/dailydeal/vendor/map/center/44.79848,20.473856/"></iframe>
</div>

Is it possible to first remove all the characters and all characters that were only the coordinates of the decimal point and so they take?

解决方案

First example:

Evaluating the following XPath 1.0 expression:

normalize-space(translate(.,translate(.,' 0123456789.', ''), ' '))

produces:

44.799439 20.483494

This can be done with any XPath engine and the context node must be the document-node, or within XSLT 1.0, as below:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <xsl:value-of select=
     "normalize-space(translate(.,translate(.,' 0123456789.', ''), ' '))
     "/>
 </xsl:template>
</xsl:stylesheet>

When applied on this XML document (the same as the provided one, but completed):

<html>
    <div id="g_map"></div>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=key" type="text/javascript"></script>
    <script type="text/javascript">
    //<![CDATA[
        var html = '';
        var lat  = 44.799439;
        var lang = 20.483494;
        if (GBrowserIsCompatible())
        {}
    ]]>
</script>
</html>

the result is:

44.799439 20.483494

Second example:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/">
  <xsl:for-each select="//*/@src">
  <xsl:value-of select=
   "normalize-space(translate(.,translate(.,' 0123456789.', ''), ' '))
   "/>
   </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

produces:

..44.7984820.473856

and needs just a little bit more cleaning.


II. More serious processing (XPath 2.0 / XSLT 2.0)

This transformation is really "universal" and works on both examples:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <xsl:analyze-string select="string-join((.,//*/@*), ' ')"
                         regex="\-?\d+\.\d+">
     <xsl:matching-substring>
       <xsl:sequence select="concat(., ' ')"/>
     </xsl:matching-substring>  
     </xsl:analyze-string>
 </xsl:template>
</xsl:stylesheet>

When applied on the second XML:

<div class="map">
    <iframe frameborder="0" border="0" scrolling="no"  marginwidth="0" 
             marginheight="0" title=""               
src="http://www.kolektiva.rs/beograd/dailydeal/vendor/map/center/44.79848,20.473856/">
</iframe>

the wanted, correct result is produced:

44.79848  20.473856 

when applied on the XML for the first example, it also produces the wanted result:

44.799439  20.483494 

这篇关于通用正则表达式代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆