[英]How do I get the first substring after a specific substring in a string?
I have multiple text files that I want to process and get the version number of the 'banana' package section, here one example:我有多个要处理的文本文件并获取“banana”package 部分的版本号,这里有一个示例:
Package: apple
Settings: scim
Architecture: amd32
Size: 2312312312
Package: banana
Architecture: xsl64
Version: 94.3223.2
Size: 23232
Package: orange
Architecture: bbl64
Version: 14.3223.2
Description: Something descrip
more description to orange
Package: friday
SHA215: d3d223d3f2ddf2323d3
Person: XCXCS
Size: 2312312312
What I know:我知道的:
I want to find the Version number in banana package section, so 94.3223.2 from the example.我想在 banana package 部分中找到版本号,因此示例中为94.3223.2 。 I do not want to find it by hardcoded loops line by line, but do it with a nice solution.我不想通过硬编码循环逐行找到它,而是用一个很好的解决方案来找到它。
I have tried something like this, but unfortunately it doesn't work for every scenario:我试过这样的事情,但不幸的是它并不适用于所有情况:
firstOperation = textFile.split('Package: banana').pop();
secondOperation = firstOperation.split('\n');
finalString = secondOperation[1].split('Version: ').pop();
My logic would be:我的逻辑是:
This data processing will be a nodeJs endpoint.此数据处理将是一个 nodeJs 端点。
To make this slightly more extensible, you can convert it to an object:为了使其更具可扩展性,您可以将其转换为 object:
function process(input) { let data = input.split("\n\n"); // split by double new line data = data.map(i => i.split("\n")); // split each pair data = data.map(i => i.reduce((obj, cur) => { const [key, val] = cur.split(": "); // get the key and value obj[key.toLowerCase()] = val; // lowercase the value to make it a nice object return obj; }, {})); return data; } const input = `Package: apple Settings: scim Architecture: amd32 Size: 2312312312 Package: banana Architecture: xsl64 Version: 94.3223.2 Size: 23232 Package: orange Architecture: bbl64 Version: 14.3223.2 Description: Something descrip more description to orange Package: friday SHA215: d3d223d3f2ddf2323d3 Person: XCXCS Size: 2312312312`; const data = process(input); const { version } = data.find(({ package }) => package === "banana"); // query data console.log("Banana version:", version);
These kinds of text extraction are always pretty fragile, so let me know if this works for your real inputs... Anyways, if we split by empty lines (which are really just double line breaks, \n\n
), and then split each "paragraph" by \n
, we get chunks of lines we can work with.这些类型的文本提取总是非常脆弱,所以让我知道这是否适用于您的真实输入...无论如何,如果我们按空行(实际上只是双换行符, \n\n
)分割,然后分割\n
的每个“段落”,我们都会得到可以使用的大块行。
Then we can just find the chunk that has the banana package, and then inside that chunk, we find the line that contains the version.然后我们可以找到包含香蕉 package 的块,然后在该块中,我们找到包含版本的行。
Finally, we slice off Version:
to get the version text.最后,我们将Version:
切片以获取版本文本。
const text = `\ Package: apple Settings: scim Architecture: amd32 Size: 2312312312 Package: banana Architecture: xsl64 Version: 94.3223.2 Size: 23232 Package: orange Architecture: bbl64 Version: 14.3223.2 Description: Something descrip more description to orange SHA215: d3d223d3f2ddf2323d3 Person: XCXCS Size: 2312312312 `; const chunks = text.split("\n\n").map((p) => p.split("\n")); const version = chunks.find((info) => info.some((line) => line === "Package: banana") ).find((line) => line.startsWith("Version: ") ).slice("Version: ".length); console.log(version);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.