c#如何截取两个关键字之间的字符串

aoye6202 2011-04-28 10:20:20
CompanyName:MEDLINE INDUSTRIES, INC. Exports TEUs: 2124905 Address:

我想获取 CompanyName:到Exports TEUs: 之间的 字符串 如何实现 求代码
...全文
2638 22 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
aoye6202 2011-04-28
  • 打赏
  • 举报
回复
谢谢大家问题解决 结贴了
朝三慕四 2011-04-28
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 advisd 的回复:]
对。一般新手都用简单明白的
string strDest=source.Substring(source.IndexOf("CompanyName:"),source.IndexOf("Exports TEUs:"));
[/Quote]

难道不可以用string 拼接的方法????
kingdom_0 2011-04-28
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 aoye6202 的回复:]

String.SubString(index,length)
第一个参数是开始的索引,后面的参数是截取的字符串的长度。这样是不可取的。

我写的代码有问题 还是 这个方法实现不了我想要的结果?
[/Quote]
string source = "CompanyName:MEDLINE INDUSTRIES, INC. Exports TEUs:2124905 Address:";
int len = "CompanyName:".Length;
int endIndex = source.IndexOf("Exports TEUs:");
aoye6202 2011-04-28
  • 打赏
  • 举报
回复
String.SubString(index,length)
第一个参数是开始的索引,后面的参数是截取的字符串的长度。这样是不可取的。

我写的代码有问题 还是 这个方法实现不了我想要的结果?
proorck6 2011-04-28
  • 打赏
  • 举报
回复
(?<=) (?=) 好像是叫“环视”,用来匹配一个位置用的。
其他两个环视是 (?<!) (?!) 是否定型的反向、正向环视。
kingdom_0 2011-04-28
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 aoye6202 的回复:]

StreamReader sr = new StreamReader(@"D:\James.ImportList\shiyong2.txt");
string source = sr.ReadToEnd();

int beginIndex = source .IndexOf("CompanyName:");
int ……
[/Quote]
String.SubString(index,length)
第一个参数是开始的索引,后面的参数是截取的字符串的长度。这样是不可取的。
紫川秀 2011-04-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 kingdom_0 的回复:]
引用 1 楼 q107770540 的回复:

string result=Regex.Match(str,"(?<=CompanyName:).*?(?=Exports TEUs:)").Value;

大兔子,能否详细解释一下这个正则……
[/Quote]
加菲猫..
kingdom_0 2011-04-28
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 xxoo2007 的回复:]

(?<=xxx) 反向预搜索(向左) 反向预搜索,判断当前位置左侧是否能够匹配指定表达式
(?=xxx) 正向预搜索(向右) 正向预搜索,判断当前位置右侧是否能匹配指定表达式
[/Quote]加菲,3Q!
aoye6202 2011-04-28
  • 打赏
  • 举报
回复
StreamReader sr = new StreamReader(@"D:\James.ImportList\shiyong2.txt");
string source = sr.ReadToEnd();

int beginIndex = source .IndexOf("CompanyName:");
int endIndex = source.IndexOf("Exports TEUs:");

string strDest = source.Substring(beginIndex, endIndex);
MessageBox.Show(strDest);
哪里有毛病??
xxoo2007 2011-04-28
  • 打赏
  • 举报
回复
(?<=xxx) 反向预搜索(向左) 反向预搜索,判断当前位置左侧是否能够匹配指定表达式
(?=xxx) 正向预搜索(向右) 正向预搜索,判断当前位置右侧是否能匹配指定表达式
aoye6202 2011-04-28
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 advisd 的回复:]
对。一般新手都用简单明白的
string strDest=source.Substring(source.IndexOf("CompanyName:"),source.IndexOf("Exports TEUs:"));
[/Quote]为什么startindex的值为-1呢?
kingdom_0 2011-04-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 q107770540 的回复:]

引用 4 楼 kingdom_0 的回复:

引用 1 楼 q107770540 的回复:

string result=Regex.Match(str,"(?<=CompanyName:).*?(?=Exports TEUs:)").Value;

大兔子,能否详细解释一下这个正则……

小蜜桃
[/Quote]
?<=和?= 不懂……
advisd 2011-04-28
  • 打赏
  • 举报
回复
对。一般新手都用简单明白的
string strDest=source.Substring(source.IndexOf("CompanyName:"),source.IndexOf("Exports TEUs:"));

aoye6202 2011-04-28
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 kingdom_0 的回复:]
引用 3 楼 linuxjava01 的回复:

int beginIndex=source.IndexOf("CompanyName:");
int endIndex=source.IndexOf("Exports TEUs:");

string strDest=source.Substring(beginIndex,endIndex-beginIndex);

大概是这样,你调……
[/Quote]我读取的就是个字符串 简单实现就好 新手 用正则有点吃力
q107770540 2011-04-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 kingdom_0 的回复:]

引用 1 楼 q107770540 的回复:

string result=Regex.Match(str,"(?<=CompanyName:).*?(?=Exports TEUs:)").Value;

大兔子,能否详细解释一下这个正则……
[/Quote]
小蜜桃
kingdom_0 2011-04-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 linuxjava01 的回复:]

int beginIndex=source.IndexOf("CompanyName:");
int endIndex=source.IndexOf("Exports TEUs:");

string strDest=source.Substring(beginIndex,endIndex-beginIndex);

大概是这样,你调试一下。
[/Quote]这种方式能实现。但是还是感觉Regex帅气!
kingdom_0 2011-04-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 q107770540 的回复:]

string result=Regex.Match(str,"(?<=CompanyName:).*?(?=Exports TEUs:)").Value;
[/Quote]
大兔子,能否详细解释一下这个正则……
linuxjava01 2011-04-28
  • 打赏
  • 举报
回复
int beginIndex=source.IndexOf("CompanyName:");
int endIndex=source.IndexOf("Exports TEUs:");

string strDest=source.Substring(beginIndex,endIndex-beginIndex);

大概是这样,你调试一下。
q107770540 2011-04-28
  • 打赏
  • 举报
回复
(?<=CompanyName[::]).*?(?=Exports TEUs[::])

注意半角全角
q107770540 2011-04-28
  • 打赏
  • 举报
回复
string result=Regex.Match(str,"(?<=CompanyName:).*?(?=Exports TEUs:)").Value;

111,088

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧