当前位置:实例文章 » C#开发实例» [文章]Java、C#、Oracle和VB6中常用的通配符和模式匹配方式的详细说明和示例

Java、C#、Oracle和VB6中常用的通配符和模式匹配方式的详细说明和示例

发布人:shili8 发布时间:2024-08-09 01:31 阅读次数:0

**Java 中的通配符和模式匹配**

在 Java 中,通配符和模式匹配是使用正则表达式(Regular Expression)实现的。Java 提供了 `java.util.regex` 包来支持正则表达式。

### 通配符Java 中的通配符包括:

* `d`: 匹配数字* `D`: 匹配非数字* `w`: 匹配字母、数字和下划线* `W`: 匹配非字母、数字和下划线* `s`: 匹配空白字符(包括空格、制表符等)
* `S`: 匹配非空白字符### 模式匹配Java 中的模式匹配可以使用 `Pattern` 和 `Matcher` 类来实现。以下是示例:

javaimport java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternExample {
 public static void main(String[] args) {
 // 匹配数字 String str = "123456";
 Pattern pattern = Pattern.compile("d");
 Matcher matcher = pattern.matcher(str);
 while (matcher.find()) {
 System.out.println(matcher.group());
 }

 // 匹配非数字 str = "abc123";
 pattern = Pattern.compile("D");
 matcher = pattern.matcher(str);
 while (matcher.find()) {
 System.out.println(matcher.group());
 }

 // 匹配字母、数字和下划线 str = "hello_world";
 pattern = Pattern.compile("w");
 matcher = pattern.matcher(str);
 while (matcher.find()) {
 System.out.println(matcher.group());
 }

 // 匹配非字母、数字和下划线 str = "!@#$%^&*()";
 pattern = Pattern.compile("W");
 matcher = pattern.matcher(str);
 while (matcher.find()) {
 System.out.println(matcher.group());
 }

 // 匹配空白字符 str = " hello ";
 pattern = Pattern.compile("s");
 matcher = pattern.matcher(str);
 while (matcher.find()) {
 System.out.println(matcher.group());
 }

 // 匹配非空白字符 str = "!@#$%^&*()";
 pattern = Pattern.compile("S");
 matcher = pattern.matcher(str);
 while (matcher.find()) {
 System.out.println(matcher.group());
 }
 }
}


### 正则表达式Java 中的正则表达式可以使用 `Pattern` 类来实现。以下是示例:

javaimport java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexExample {
 public static void main(String[] args) {
 // 匹配数字 String str = "123456";
 Pattern pattern = Pattern.compile("d+");
 Matcher matcher = pattern.matcher(str);
 while (matcher.find()) {
 System.out.println(matcher.group());
 }

 // 匹配非数字 str = "abc123";
 pattern = Pattern.compile("D+");
 matcher = pattern.matcher(str);
 while (matcher.find()) {
 System.out.println(matcher.group());
 }

 // 匹配字母、数字和下划线 str = "hello_world";
 pattern = Pattern.compile("w+");
 matcher = pattern.matcher(str);
 while (matcher.find()) {
 System.out.println(matcher.group());
 }

 // 匹配非字母、数字和下划线 str = "!@#$%^&*()";
 pattern = Pattern.compile("W+");
 matcher = pattern.matcher(str);
 while (matcher.find()) {
 System.out.println(matcher.group());
 }

 // 匹配空白字符 str = " hello ";
 pattern = Pattern.compile("s+");
 matcher = pattern.matcher(str);
 while (matcher.find()) {
 System.out.println(matcher.group());
 }

 // 匹配非空白字符 str = "!@#$%^&*()";
 pattern = Pattern.compile("S+");
 matcher = pattern.matcher(str);
 while (matcher.find()) {
 System.out.println(matcher.group());
 }
 }
}


### 模式匹配Java 中的模式匹配可以使用 `Matcher` 类来实现。以下是示例:

javaimport java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MatcherExample {
 public static void main(String[] args) {
 // 匹配数字 String str = "123456";
 Pattern pattern = Pattern.compile("d+");
 Matcher matcher = pattern.matcher(str);
 while (matcher.find()) {
 System.out.println(matcher.group());
 }

 // 匹配非数字 str = "abc123";
 pattern = Pattern.compile("D+");
 matcher = pattern.matcher(str);
 while (matcher.find()) {
 System.out.println(matcher.group());
 }

 // 匹配字母、数字和下划线 str = "hello_world";
 pattern = Pattern.compile("w+");
 matcher = pattern.matcher(str);
 while (matcher.find()) {
 System.out.println(matcher.group());
 }

 // 匹配非字母、数字和下划线 str = "!@#$%^&*()";
 pattern = Pattern.compile("W+");
 matcher = pattern.matcher(str);
 while (matcher.find()) {
 System.out.println(matcher.group());
 }

 // 匹配空白字符 str = " hello ";
 pattern = Pattern.compile("s+");
 matcher = pattern.matcher(str);
 while (matcher.find()) {
 System.out.println(matcher.group());
 }

 // 匹配非空白字符 str = "!@#$%^&*()";
 pattern = Pattern.compile("S+");
 matcher = pattern.matcher(str);
 while (matcher.find()) {
 System.out.println(matcher.group());
 }
 }
}


**C# 中的通配符和模式匹配**

在 C# 中,通配符和模式匹配是使用正则表达式(Regular Expression)实现的。C# 提供了 `System.Text.RegularExpressions` 名称空间来支持正则表达式。

### 通配符C# 中的通配符包括:

* `d`: 匹配数字* `D`: 匹配非数字* `w`: 匹配字母、数字和下划线* `W`: 匹配非字母、数字和下划线* `s`: 匹配空白字符(包括空格、制表符等)
* `S`: 匹配非空白字符### 模式匹配C# 中的模式匹配可以使用 `Regex` 类来实现。以下是示例:

csharpusing System.Text.RegularExpressions;

class RegexExample {
 static void Main() {
 // 匹配数字 string str = "123456";
 Regex regex = new Regex("d+");
 MatchCollection matches = regex.Matches(str);
 foreach (Match match in matches) {
 Console.WriteLine(match.Value);
 }

 // 匹配非数字 str = "abc123";
 regex = new Regex("D+");
 matches = regex.Matches(str);
 foreach (Match match in matches) {
 Console.WriteLine(match.Value);
 }

 // 匹配字母、数字和下划线 str = "hello_world";
 regex = new Regex("w+");
 matches = regex.Matches(str);
 foreach (Match match in matches) {
 Console.WriteLine(match.Value);
 }

 // 匹配非字母、数字和下划线 str = "!@#$%^&*()";
 regex = new Regex("W+");
 matches = regex.Matches(str);
 foreach (Match match in matches) {
 Console.WriteLine(match.Value);
 }

 // 匹配空白字符 str = " hello ";
 regex = new Regex("s+");
 matches = regex.Matches(str);
 foreach (Match match in matches) {
 Console.WriteLine(match.Value);
 }

 // 匹配非空白字符 str = "!@#$%^&*()";
 regex = new Regex("S+");
 matches = regex.Matches(str);
 foreach (Match match in matches) {
 Console.WriteLine(match.Value);
 }
 }
}


### 正则表达式C# 中的正则表达式可以使用 `Regex` 类来实现。以下是示例:

csharpusing System.Text.RegularExpressions;

class RegexExample {
 static void Main() {
 // 匹配数字 string str = "123456";
 Regex regex = new Regex("d+");
 MatchCollection matches = regex.Matches(str);
 foreach (Match match in matches) {
 Console.WriteLine(match.Value);
 }

 // 匹配非数字 str = "abc123";
 regex = new Regex("D+");
 matches = regex.Matches(str);
 foreach (Match match in matches) {
 Console.WriteLine(match.Value);
 }

 // 匹配字母、数字和下划线 str = "hello_world";
 regex = new Regex("w+");
 matches = regex.Matches(str);
 foreach (Match match in

其他信息

其他资源

Top