regex 使用正则表达式在文本中查找数字

oknwwptz  于 2023-02-05  发布在  其他
关注(0)|答案(1)|浏览(145)

我试图找到一种方法来提取数字(xxx.xxxx)从这个文本文件(是一个gml cityJson文件):

</bldg:lod2Solid>
      <bldg:lod2TerrainIntersection>
        <gml:MultiCurve>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371139.98 5808526.986 68.01 371139.991 5808526.991 68.01 371140 5808526.995 68.01 371140.01 5808527 68.01 371140.378 5808527.169 68.01</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371140.378 5808527.169 68.01 371140.809 5808527.245 68.01</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371140.809 5808527.245 68.01 371141 5808527.229 68.01 371141.212 5808527.212 68.008 371141.245 5808527.209 68.006</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371141.245 5808527.209 68.006 371141.657 5808527.063 67.986</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371141.657 5808527.063 67.986 371141.75 5808527 67.98 371141.899 5808526.899 67.977 371142 5808526.83 67.973 371142.019 5808526.817 67.974</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371142.019 5808526.817 67.974 371142.306 5808526.486 67.993</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371142.306 5808526.486 67.993 371142.365 5808526.365 67.997 371142.499 5808526.094 67.988</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371142.499 5808526.094 67.988 371142.518 5808526 67.985 371142.586 5808525.665 67.998</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371142.561 5808525.228 67.982 371142.581 5808525.581 68.001 371142.586 5808525.665 67.998</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371142.386 5808524.801 67.992 371142.468 5808525 67.979 371142.561 5808525.228 67.982</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371142.107 5808524.435 68.03 371142.386 5808524.801 67.992</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371141.743 5808524.152 68.057 371142 5808524.352 68.043 371142.107 5808524.435 68.03</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371141.318 5808523.973 68.077 371141.382 5808524 68.075 371141.743 5808524.152 68.057</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371140.862 5808523.91 68.09 371140.918 5808523.918 68.09 371141 5808523.929 68.089 371141.318 5808523.973 68.077</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371140.404 5808523.968 68.096 371140.862 5808523.91 68.09</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371139.977 5808524.141 68.095 371140 5808524.132 68.096 371140.094 5808524.094 68.097 371140.325 5808524 68.097 371140.404 5808523.968 68.096</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371139.61 5808524.419 68.072 371139.977 5808524.141 68.095</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371139.326 5808524.782 68.063 371139.526 5808524.526 68.065 371139.61 5808524.419 68.072</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371137.811 5808525.146 68.103 371138 5808525.101 68.111 371138.081 5808525.081 68.113 371138.419 5808525 68.095 371138.887 5808524.887 68.074 371139 5808524.86 68.06 371139.326 5808524.782 68.063</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371137.811 5808525.146 68.103 371137.896 5808525.896 68.037 371137.908 5808526 68.035 371137.995 5808526.769 68.015</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371137.995 5808526.769 68.015 371138 5808526.769 68.015 371138.741 5808526.741 68.015 371139 5808526.731 68.015 371139.643 5808526.707 68.016</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371139.643 5808526.707 68.016 371139.98 5808526.986 68.01</gml:posList>
            </gml:LineString>
          </gml:curveMember>
        </gml:MultiCurve>
      </bldg:lod2TerrainIntersection>
      <bldg:boundedBy>
        <bldg:WallSurface gml:id="UUID_776cbf08-4447-4c77-8c77-4ea39b283a81">
          <core:creationDate>2022-03-03</core:creationDate>
          <bldg:lod2MultiSurface>
            <gml:MultiSurface gml:id="UUID_3d4e234e-add8-43d9-b90c-abbcbbe6073c">
              <gml:surfaceMember>

我的计划是首先通过regex /<bldg:lod2TerrainIntersection>(.*?)<\/bldg:lod2TerrainIntersection>/g找到<bldg:lod2TerrainIntersection></bldg:lod2TerrainIntersection>之间的所有内容,但没有找到任何内容...然后计划通过regex提取数字:\d+\.\d+提取www.example.com的所有号码xxx.xxx也不起作用:/
我做错了什么?或者有更好的方法吗?
非常感谢你的帮助!

a0x5cqrl

a0x5cqrl1#

由于正则表达式在解析嵌套内容(如xml文档)时不可靠,因此更好地满足您的期望的最佳选择是使用DOMParser对象,只要Javascript是执行此工作的运行时。
这里我展示了一个函数,该函数将解析一个给定的xml文本内容,并检索每个lod2TerrainIntersection父节点中的所有posList节点,结果将作为每个父元素的字符串数组返回。
这样posList标签值就被提取出来了,所有这些“数字”都被隔离为数组的值,现在一旦你有了这些值,你就可以根据自己的喜好解析它们了。

function parseContent(xmlcontent){
  //parse the xml document
  const parser = new DOMParser();
  const xml = parser.parseFromString(xmlcontent, "text/xml");

  //select elements having tagname bldg:lod2TerrainIntersection
  const nodes_lod2TerrainIntersection = xml.getElementsByTagName('bldg:lod2TerrainIntersection');
  
  //fetch the result as an array of collections of posList values for each parent container
  const values = [...nodes_lod2TerrainIntersection].map(parentNode => {
    //select all the descendant being gml:postList
    const nodes_posList = parentNode.getElementsByTagName('gml:posList');
    //return the array of values coming from all those nodes
    return [...nodes_posList].map( node => node.textContent );    
  });
  
  return values;
}

以下是为单个父节点返回的数组示例:

[
    "371139.98 5808526.986 68.01 371139.991 5808526.991 68.01 371140 5808526.995 68.01 371140.01 5808527 68.01 371140.378 5808527.169 68.01",
    "371140.378 5808527.169 68.01 371140.809 5808527.245 68.01",
    "371140.809 5808527.245 68.01 371141 5808527.229 68.01 371141.212 5808527.212 68.008 371141.245 5808527.209 68.006",
    ...
]
function parseContent(xmlcontent){
  //parse the xml document
  const parser = new DOMParser();
  const xml = parser.parseFromString(xmlcontent, "text/xml");

  //select elements having tagname bldg:lod2TerrainIntersection
  const nodes_lod2TerrainIntersection = xml.getElementsByTagName('bldg:lod2TerrainIntersection');
  
  //fetch the result as an array of collections of posList values for each parent container
  const values = [...nodes_lod2TerrainIntersection].map(parentNode => {
    //select all the descendant being gml:postList
    const nodes_posList = parentNode.getElementsByTagName('gml:posList');
    //return the array of values coming from all those nodes
    return [...nodes_posList].map( node => node.textContent );    
  });
  
  return values;
}

//I added a root element to your xml just to add some xml consistency
const filecontent = `<?xml version="1.0" encoding="UTF-8"?>
<core:Model
  xmlns:bldg="http://www.opengis.net/citygml/building/2.0"
  xmlns:gml="http://www.opengis.net/gml"
  xmlns:core="http://www.opengis.net/citygml/2.0">

 <bldg:lod2TerrainIntersection>
        <gml:MultiCurve>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371139.98 5808526.986 68.01 371139.991 5808526.991 68.01 371140 5808526.995 68.01 371140.01 5808527 68.01 371140.378 5808527.169 68.01</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371140.378 5808527.169 68.01 371140.809 5808527.245 68.01</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371140.809 5808527.245 68.01 371141 5808527.229 68.01 371141.212 5808527.212 68.008 371141.245 5808527.209 68.006</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371141.245 5808527.209 68.006 371141.657 5808527.063 67.986</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371141.657 5808527.063 67.986 371141.75 5808527 67.98 371141.899 5808526.899 67.977 371142 5808526.83 67.973 371142.019 5808526.817 67.974</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371142.019 5808526.817 67.974 371142.306 5808526.486 67.993</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371142.306 5808526.486 67.993 371142.365 5808526.365 67.997 371142.499 5808526.094 67.988</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371142.499 5808526.094 67.988 371142.518 5808526 67.985 371142.586 5808525.665 67.998</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371142.561 5808525.228 67.982 371142.581 5808525.581 68.001 371142.586 5808525.665 67.998</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371142.386 5808524.801 67.992 371142.468 5808525 67.979 371142.561 5808525.228 67.982</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371142.107 5808524.435 68.03 371142.386 5808524.801 67.992</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371141.743 5808524.152 68.057 371142 5808524.352 68.043 371142.107 5808524.435 68.03</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371141.318 5808523.973 68.077 371141.382 5808524 68.075 371141.743 5808524.152 68.057</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371140.862 5808523.91 68.09 371140.918 5808523.918 68.09 371141 5808523.929 68.089 371141.318 5808523.973 68.077</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371140.404 5808523.968 68.096 371140.862 5808523.91 68.09</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371139.977 5808524.141 68.095 371140 5808524.132 68.096 371140.094 5808524.094 68.097 371140.325 5808524 68.097 371140.404 5808523.968 68.096</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371139.61 5808524.419 68.072 371139.977 5808524.141 68.095</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371139.326 5808524.782 68.063 371139.526 5808524.526 68.065 371139.61 5808524.419 68.072</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371137.811 5808525.146 68.103 371138 5808525.101 68.111 371138.081 5808525.081 68.113 371138.419 5808525 68.095 371138.887 5808524.887 68.074 371139 5808524.86 68.06 371139.326 5808524.782 68.063</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371137.811 5808525.146 68.103 371137.896 5808525.896 68.037 371137.908 5808526 68.035 371137.995 5808526.769 68.015</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371137.995 5808526.769 68.015 371138 5808526.769 68.015 371138.741 5808526.741 68.015 371139 5808526.731 68.015 371139.643 5808526.707 68.016</gml:posList>
            </gml:LineString>
          </gml:curveMember>
          <gml:curveMember>
            <gml:LineString>
              <gml:posList srsDimension="3">371139.643 5808526.707 68.016 371139.98 5808526.986 68.01</gml:posList>
            </gml:LineString>
          </gml:curveMember>
        </gml:MultiCurve>
      </bldg:lod2TerrainIntersection>

</core:Model>`;  

const values = parseContent(filecontent);
console.log(values);

相关问题