在指定字符后搜索单词 [英] Search for word after specified character

查看:59
本文介绍了在指定字符后搜索单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have a text file that contains several words
...........
...........  dimension="13.7mm" row="30" speed="13.4" ........


I want to extract the value 13.7mm
This is what I have done 
//loaded the file
string file = "c:\\temp\\myTextFile.txt";
string searchText = "dimension=";
//Declare reader as a new StreamReader with file as the file to use  

    StreamReader reader = StreamReader(file);  

    
//Declare text as the reader reading to the end  

     String text = reader.ReadToEnd();  

     //If the searchText is a match  

     if (Regex.IsMatch(text, searchText))  

     {
        
         MessageBox.Show("found it");

       //OK now that I found the word 'dimension="
       //how to display the next characters between the next "  and " 
       //the number of characters may vary (e.g. 125.7mm)
            
    }





我尝试过:



试图拆分它



What I have tried:

tried to split it

sring[] lines = Regex.Split("server=", " ");

         foreach (string line in lines)
         {
             listBox1.Items.Add(line);
            
         }

推荐答案

在正则表达式中使用 capture 。有关代码示例,请参阅此处:Regex Groups - Dot Net Perls [ ^ ]
Use a capture in your regular expression. See here for code sample: Regex Groups - Dot Net Perls[^]


这篇关于在指定字符后搜索单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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