游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3607|回复: 6

帮帮忙找一段在一段文本中按正则表达式查找字符串的代码

[复制链接]

187

主题

6490

帖子

6491

积分

论坛元老

团长

Rank: 8Rank: 8

积分
6491
发表于 2007-9-2 12:14:00 | 显示全部楼层 |阅读模式
最好是不要“Microsoft VBScript Regular Expressions 5.5”的那种,谢谢~~
[em20]

22

主题

371

帖子

387

积分

中级会员

Rank: 3Rank: 3

积分
387
发表于 2007-9-2 12:56:00 | 显示全部楼层

Re:帮帮忙找一段在一段文本中按正则表达式查找字符串的

?
类库里面包含了regex类了啊
Dim Regex1 as new system.text.RegularExpressions.Regex("这里填正则表达式")
Dim Match1 as System.Text.RegularExpressions.Match = Regex1.Match("被查找的文本")
然后match1就可以返回成功与否或是查到的位置
也可以用Regex类的静态方法,有Replace和Match等等的可以用

187

主题

6490

帖子

6491

积分

论坛元老

团长

Rank: 8Rank: 8

积分
6491
 楼主| 发表于 2007-9-2 13:07:00 | 显示全部楼层

Re:帮帮忙找一段在一段文本中按正则表达式查找字符串的

怎么实现的没有详细的代码吗?

0

主题

25

帖子

25

积分

注册会员

Rank: 2

积分
25
发表于 2007-9-2 16:48:00 | 显示全部楼层

Re:帮帮忙找一段在一段文本中按正则表达式查找字符串的

?
MSDN
          .NET Framework 类库  
Regex 类  
请参见  示例  成员
全部折叠 全部展开    语言筛选器: 全部 语言筛选器: 多个 语言筛选器: Visual Basic 语言筛选器: C# 语言筛选器: C++ 语言筛选器: J# 语言筛选器: JScript  
Visual Basic(声明)
Visual Basic(用法)
C#
C++
J#
JScript
表示不可变的正则表达式。

命名空间:System.Text.RegularExpressions
程序集:System(在 system.dll 中)

语法
Visual Basic(声明)
<SerializableAttribute> _
Public Class Regex
        Implements ISerializable

Visual Basic(用法)
Dim instance As Regex


C#
[SerializableAttribute]
public class Regex : ISerializable

C++
[SerializableAttribute]
public ref class Regex : ISerializable

J#
/** @attribute SerializableAttribute() */
public class Regex implements ISerializable

JScript
SerializableAttribute
public class Regex implements ISerializable


备注
Regex 类包含若干静态方法,使您无需显式创建 Regex 对象即可使用正则表达式。使用静态方法等效于构造 Regex 对象,使用该对象一次然后将其销毁。

Regex 类是不可变(只读)的,并且具有固有的线程安全性。可以在任何线程上创建 Regex 对象,并在线程间共享。有关更多信息,请参见线程安全。

示例
下面的代码示例演示如何使用正则表达式检查字符串是否具有表示货币值的正确格式。注意,如果使用 ^ 和 $ 封闭标记,则指示整个字符串(而不只是子字符串)都必须匹配正则表达式。

C#  复制代码
using System;
using System.Text.RegularExpressions;

public class Test
{

    public static void Main ()
    {

              // Define a regular expression for currency values.
              Regex rx = new Regex(@"^-?\d+(\.\d{2})?$");
              
              // Define some test strings.
              string[] tests = {"-42", "19.99", "0.001", "100 USD"};
              
              // Check each test string against the regular expression.
              foreach (string test in tests)
              {
                  if (rx.IsMatch(test))
                  {
                      Console.WriteLine("{0} is a currency value.", test);
                  }
                  else
                  {
                      Console.WriteLine("{0} is not a currency value.", test);
                  }
              }
            
    }       
       
}


C++  复制代码
#using <System.dll>

using namespace System;
using namespace System::Text::RegularExpressions;
int main()
{
   
   // Define a regular expression for currency values.
   Regex^ rx = gcnew Regex( "^-?\\d+(\\.\\d{2})?$" );
   
   // Define some test strings.
   array<String^>^tests = {"-42","19.99","0.001","100 USD"};
   
   // Check each test string against the regular expression.
   System::Collections::IEnumerator^ myEnum = tests->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      String^ test = safe_cast<String^>(myEnum->Current);
      if ( rx->IsMatch( test ) )
      {
         Console::WriteLine( "{0} is a currency value.", test );
      }
      else
      {
         Console::WriteLine( "{0} is not a currency value.", test );
      }
   }
}



J#  复制代码
import System.*;
import System.Text.RegularExpressions.*;

public class Test
{
    public static void main(String[] args)
    {
        // Define a regular expression for currency values.
        Regex rx = new Regex("^-?\\d+(\\.\\d{2})?$");

        // Define some test strings.
        String tests[] =  { "-42", "19.99", "0.001", "100 USD" };

        // Check each test string against the regular expression.
        for (int iCtr = 0; iCtr < tests.get_Length(); iCtr++) {
            String test = (String)tests.get_Item(iCtr);
            if (rx.IsMatch(test)) {
                Console.WriteLine("{0} is a currency value.", test);
            }
            else {
                Console.WriteLine("{0} is not a currency value.", test);
            }
        }
    } //main
} //Test



下面的代码示例演示如何使用正则表达式检查字符串中重复出现的词。注意如何使用 (?<word>) 构造来命名组,以及稍后如何使用 (\k<word>) 在表达式中引用该组。

C#  复制代码
using System;
using System.Text.RegularExpressions;

public class Test
{

    public static void Main ()
    {

        // Define a regular expression for repeated words.
        Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
          RegexOptions.Compiled | RegexOptions.IgnoreCase);

        // Define a test string.        
        string text = "The the quick brown fox  fox jumped over the lazy dog dog.";
        
        // Find matches.
        MatchCollection matches = rx.Matches(text);

        // Report the number of matches found.
        Console.WriteLine("{0} matches found.", matches.Count);

        // Report on each match.
        foreach (Match match in matches)
        {
            string word = match.Groups["word"].Value;
            int index = match.Index;
            Console.WriteLine("{0} repeated at position {1}", word, index);   
        }
        
    }
       
}


C++  复制代码
#using <System.dll>

using namespace System;
using namespace System::Text::RegularExpressions;
int main()
{
   // Define a regular expression for repeated words.
   Regex^ rx = gcnew Regex( "\\b(?<word>\\w+)\\s+(\\k<word>)\\b",static_cast<RegexOptions>(RegexOptions::Compiled | RegexOptions::IgnoreCase) );

   // Define a test string.        
   String^ text = "The the quick brown fox  fox jumped over the lazy dog dog.";

   // Find matches.
   MatchCollection^ matches = rx->Matches( text );

   // Report the number of matches found.
   Console::WriteLine( "{0} matches found.", matches->Count );

   // Report on each match.
   for each (Match^ match in matches)
   {
      String^ word = match->Groups["word"]->Value;
      int index = match->Index;
      Console::WriteLine("{0} repeated at position {1}", word, index);   
   }
}


J#  复制代码
import System.*;
import System.Text.RegularExpressions.*;

public class Test
{
    public static void main(String[] args)
    {
        // Define a regular expression for repeated words.
        Regex rx = new Regex("\\b(?<word>\\w+)\\s+(\\k<word>)\\b",
            RegexOptions.Compiled | RegexOptions.IgnoreCase);

        // Define a test string.        
        String text = "The the quick brown fox  fox jumped over the "
            + "lazy dog dog.";

        // Find matches.
        MatchCollection matches = rx.Matches(text);

        // Report the number of matches found.
        Console.WriteLine("{0} matches found.", (Int32)matches.get_Count());

        // Report on each match.
        for (int iCtr = 0; iCtr < matches.get_Count(); iCtr++) {
            Match match = matches.get_Item(iCtr);
            String word = match.get_Groups().get_Item("word").get_Value();
            int index = match.get_Index();
            Console.WriteLine("{0} repeated at position {1}", word,
                (Int32)index);
        }
    } //main      
} //Test



继承层次结构
System.Object
  System.Text.RegularExpressions.Regex
     派生类

线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。

版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0

.NET Compact Framework
受以下版本支持:2.0、1.0

请参见
参考
Regex 成员
System.Text.RegularExpressions 命名空间

其他资源
.NET Framework 正则表达式
正则表达式语言元素

如果您对本产品的帮助或其他功能有任何建议或需要报告 Bug,请转到反馈站点。

187

主题

6490

帖子

6491

积分

论坛元老

团长

Rank: 8Rank: 8

积分
6491
 楼主| 发表于 2007-9-2 17:43:00 | 显示全部楼层

Re:帮帮忙找一段在一段文本中按正则表达式查找字符串的

I need one what codec by VB6.........

29

主题

475

帖子

483

积分

中级会员

Rank: 3Rank: 3

积分
483
发表于 2007-9-2 22:30:00 | 显示全部楼层

Re:帮帮忙找一段在一段文本中按正则表达式查找字符串的

VB6 's I no clear,floor up 's copy 's very 辛苦

187

主题

6490

帖子

6491

积分

论坛元老

团长

Rank: 8Rank: 8

积分
6491
 楼主| 发表于 2007-9-3 12:19:00 | 显示全部楼层

Re:帮帮忙找一段在一段文本中按正则表达式查找字符串的

I know that.....
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-25 02:44

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表