需要在特定的行或单词后找到单词 [英] Need to find word after specific line or word
问题描述
我有以下字符串文字。
一个5
=============================================== =====
A:\ Hello.txt -test
==================== ================================
One 1 Two 2 Three 3
以上字符串,我想找到One 1(将在===行之后)
请建议
Hi,
I have below string text.
One 5
====================================================
A:\Hello.txt -test
====================================================
One 1 Two 2 Three 3
with above string, I want to find "One 1"(which will be after "===" line)
Kindly suggest
推荐答案
使用此正则表达式,确保未打开Multiline选项:
Use this regex, making sure that the Multiline option is NOT turned on:
\n=+\nOne
在默认模式下,您可以将换行符与 \ n
并继续从一行到下一行匹配。所以我的正则表达式寻找换行符后跟所有=直到另一个换行符,然后单词One。
In the default mode, you can match newlines with \n
and continue matching from one line to the next. So my regex looks for a newline followed by all "=" until another newline, then the word "One".
如果你的模式没有改变,你可以使用 String.Substring方法 [ ^ ]和 String.LastIndexOf方法 [ ^ ]找到它。
对于Ex - 假设上面的字符串存储在变量str中。使用上面的方法你可以编写类似
If your patter doesn't change, you can use combination of String.Substring Method[^] and String.LastIndexOf Method[^] to find it.
For Ex - suppose the above string is stored in a variable str. Using above methods you could write something like
str.Substring(str.LastIndexOf("==="), str.IndexOf(" ") + 1);
请注意,这只是一个例子。虽然它会为您的问题提供正确的输出,但您必须根据您的要求进行微调(例如,如果您有2位或更多位数字)。
希望这给了你一个想法。
Please note that this is just an example. Though it will give a correct output for your question, you must fine tune it based on your requirement (for ex if you have 2 or more digit numbers).
Hope this gives you an idea.
这篇关于需要在特定的行或单词后找到单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!