universal regex code

◇◆丶佛笑我妖孽 提交于 2019-12-11 07:20:13

问题


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?


回答1:


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 




回答2:


I'm not 100% sure that I interpret your question correctly. But if what you want is to extract the numbers 44.79848 and 20.473856 from the two snippets you have provided above (not varying much else than above) then yes, no problem.

The more generic you want it to be, the more less fitting I would say it is to use regular expressions.

I think you have to be way more specific in your question if you want more constructive answers that can actually help you.



来源:https://stackoverflow.com/questions/7932528/universal-regex-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!