在 Groovy 中使用正则表达式在 Groovy 中使用正则表达式在 Groovy 中使用正则表达式在 Groovy 中使用正则表达式
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
在 Groovy 中使用正则表达式
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式工具和实用程序 » 在 Groovy 中使用正则表达式

正则表达式工具
grep
语言和函数库
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery 和 XPath
XRegExp
数据库
MySQL
Oracle
PostgreSQL
本网站的更多内容
简介
正则表达式快速入门
正则表达式教程
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

在 Groovy 中使用正则表达式

由于 Groovy 是基于 Java,因此您可以在 Groovy 中使用 Java 的正则表达式套件。只要在 Groovy 原代码的最上方加上 import java.util.regex.*。使用正则表达式的任何 Java 代码也会自动在您的 Groovy 代码中运作。

使用冗长的 Java 代码在 Groovy 中处理正则表达式并不会很 groovy。Groovy 有一堆语言功能,可以让使用正则表达式的代码简洁许多。您可以将 Groovy 专属语法与一般的 Java 代码混合使用。所有内容都基于 java.util.regex 套件,您无论如何都需要导入这个套件。

Groovy 字符串

Java 只有单一字符串样式。字符串置于双引号之间。字符串中的双引号和反斜线必须以反斜线作为转义字符。这会在正则表达式文本中产生一大堆反斜线。

Groovy 有五种字符串样式。字符串可以置於单引号、双引号、三重单引号和三重双引号之间。使用三重单引号或双引号可以让字符串跨越多行,这对于自由间距的正则表达式非常方便。遗憾的是,这四种字符串样式都需要反斜线作为转义字符。

第五种字符串样式特别提供给正则表达式。字符串置于正斜线之间,且字符串中的正斜线(而非反斜线)才需要转义。这的确是一种字符串样式。/hello/ 和 "hello" 都是 java.lang.String 的字面实例。遗憾的是,以正斜线分隔的字符串无法跨行,因此无法将其用于自由间距的正则表达式。

Groovy 模式和比对器

若要实际将字符串用作正则表达式,您需要实例化 java.util.regex.Pattern 类别。若要实际对字符串使用该模式,您需要实例化 java.util.regex.Matcher 类别。您在 Groovy 中使用这些类别的方式与在 Java 中相同。但 Groovy 提供了一些特殊语法,让您可以用更少的输入创建这些实例。

若要创建 Pattern 实例,只需在包含正则表达式的字符串前面加上波浪号。字符串可以使用 Groovy 的五种字符串样式中的任何一种。将此模式指派给变量时,请务必在指定操作符与波浪号之间留一个空格。

Pattern myRegex = ~/regex/

您实际上不会经常以这种方式实例化模式。您唯一需要 Pattern 实例的时候是分割字符串,这需要您调用 Pattern.split()。若要寻找正则表达式比对或使用正则表达式进行搜索和取代,您需要一个 Matcher 实例,将模式绑定到字符串。在 Groovy 中,您可以使用 =~ 操作符直接从包含正则表达式的字面字符串创建此实例。这次 = 和 ~ 之间没有空格。

Matcher myMatcher = "subject" =~ /regex/

这是

Matcher myMatcher = Pattern.compile(/regex/).matcher("subject")

最后,==~ 操作符是测试正则表达式是否能完全比对字符串的快速方式。myString ==~ /regex/ 等同于 myString.matches(/regex/)。若要寻找部分比对,您需要使用 Matcher。

在 Groovy 中使用正規表示式
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式工具和實用程式 » 在 Groovy 中使用正規表示式

正規表示式工具
grep
語言和函式庫
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery 和 XPath
XRegExp
資料庫
MySQL
Oracle
PostgreSQL
本網站的更多內容
簡介
正規表示式快速入門
正規表示式教學
替換字串教學
應用程式和語言
正規表示式範例
正規表示式參考
替換字串參考

在 Groovy 中使用正規表示式

由於 Groovy 是基於 Java,因此您可以在 Groovy 中使用 Java 的正規表示式套件。只要在 Groovy 原始碼的最上方加上 import java.util.regex.*。使用正規表示式的任何 Java 程式碼也會自動在您的 Groovy 程式碼中運作。

使用冗長的 Java 程式碼在 Groovy 中處理正規表示式並不會很 groovy。Groovy 有一堆語言功能,可以讓使用正規表示式的程式碼簡潔許多。您可以將 Groovy 專屬語法與一般的 Java 程式碼混合使用。所有內容都基於 java.util.regex 套件,您無論如何都需要匯入這個套件。

Groovy 字串

Java 只有單一字串樣式。字串置於雙引號之間。字串中的雙引號和反斜線必須以反斜線作為跳脫字元。這會在正規表示式文字中產生一大堆反斜線。

Groovy 有五種字串樣式。字串可以置於單引號、雙引號、三重單引號和三重雙引號之間。使用三重單引號或雙引號可以讓字串跨越多行,這對於自由間距的正規表示式非常方便。遺憾的是,這四種字串樣式都需要反斜線作為跳脫字元。

第五種字串樣式特別提供給正規表示式。字串置於正斜線之間,且字串中的正斜線(而非反斜線)才需要跳脫。這的確是一種字串樣式。/hello/ 和 "hello" 都是 java.lang.String 的字面實例。遺憾的是,以正斜線分隔的字串無法跨行,因此無法將其用於自由間距的正規表示式。

Groovy 模式和比對器

若要實際將字串用作正規表示式,您需要實例化 java.util.regex.Pattern 類別。若要實際對字串使用該模式,您需要實例化 java.util.regex.Matcher 類別。您在 Groovy 中使用這些類別的方式與在 Java 中相同。但 Groovy 提供了一些特殊語法,讓您可以用更少的輸入建立這些實例。

若要建立 Pattern 實例,只需在包含正規表示式的字串前面加上波浪號。字串可以使用 Groovy 的五種字串樣式中的任何一種。將此模式指派給變數時,請務必在指定運算子與波浪號之間留一個空格。

Pattern myRegex = ~/regex/

您實際上不會經常以這種方式實例化模式。您唯一需要 Pattern 實例的時候是分割字串,這需要您呼叫 Pattern.split()。若要尋找正規表示式比對或使用正規表示式進行搜尋和取代,您需要一個 Matcher 實例,將模式繫結到字串。在 Groovy 中,您可以使用 =~ 運算子直接從包含正規表示式的字面字串建立此實例。這次 = 和 ~ 之間沒有空格。

Matcher myMatcher = "subject" =~ /regex/

這是

Matcher myMatcher = Pattern.compile(/regex/).matcher("subject")

最後,==~ 運算子是測試正規表示式是否能完全比對字串的快速方式。myString ==~ /regex/ 等同於 myString.matches(/regex/)。若要尋找部分比對,您需要使用 Matcher。

Using Regular Expressions in Groovy
  • 简
  • 繁
  • En
About Regular Expressions » Tools and Utilities for Regular Expressions » Using Regular Expressions in Groovy

Regex Tools
grep
Languages & Libraries
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery & XPath
XRegExp
Databases
MySQL
Oracle
PostgreSQL
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference

Using Regular Expressions in Groovy

Because Groovy is based on Java, you can use Java’s regular expression package with Groovy. Simply put import java.util.regex.* at the top of your Groovy source code. Any Java code using regular expressions will then automatically work in your Groovy code too.

Using verbose Java code to work with regular expressions in Groovy wouldn’t be very groovy. Groovy has a bunch of language features that make code using regular expressions a lot more concise. You can mix the Groovy-specific syntax with regular Java code. It’s all based in the java.util.regex package, which you’ll need to import regardless.

Groovy Strings

Java has only one string style. Strings are placed between double quotes. Double quotes and backslashes in strings must be escaped with backslashes. That yields a forest of backslashes in literal regular expressions.

Groovy has five string styles. Strings can be placed between single quotes, double quotes, triple single quotes, and triple double quotes. Using triple single or double quotes allows the string to span multiple lines, which is handy for free-spacing regular expressions. Unfortunately, all four of these string styles require backslashes to be escaped.

The fifth string style is provided specifically for regular expressions. The string is placed between forward slashes, and only forward slashes (not backslashes) in the string need to be escaped. This is indeed a string style. Both /hello/ and "hello" are literal instances of java.lang.String. Unfortunately, strings delimited with forward slashes cannot span across lines, so you can’t use them for free-spacing regular expressions.

Groovy Patterns and Matchers

To actually use a string as a regular expression, you need to instantiate the java.util.regex.Pattern class. To actually use that pattern on a string, you need to instantiate the java.util.regex.Matcher class. You use these classes in Groovy just like you do in Java. But Groovy does provide some special syntax that allows you to create those instances with much less typing.

To create a Pattern instance, simply place a tilde before the string with your regular expression. The string can use any of Groovy’s five string styles. When assigning this pattern to a variable, make sure to leave a space between the assignment operator and the tilde.

Pattern myRegex = ~/regex/

You won’t actually instantiate patterns this way very often. The only time you need the Pattern instance is to split a string, which requires you to call Pattern.split(). To find regex matches or to search-and-replace with a regular expression, you need a Matcher instance that binds the pattern to a string. In Groovy, you can create this instance directly from the literal string with your regular expression using the =~ operator. No space between the = and ~ this time.

Matcher myMatcher = "subject" =~ /regex/

This short for:

Matcher myMatcher = Pattern.compile(/regex/).matcher("subject")

Finally, the ==~ operator is a quick way to test whether a regex can match a string entirely. myString ==~ /regex/ is equivalent to myString.matches(/regex/). To find partial matches, you need to use the Matcher.

©2015-2025 艾丽卡 support@alaica.com