WagTools ロゴ

正規表現テスト・解析ツール

正規表現

フラグ

テキスト

一致テキスト

コード例

一般的なプログラミング言語で正規表現を使用します。

JavaScript

一致するか確認

// Check if the text matches
const isMatch = /pattern/.test(text);

一致内容を取得

// Get the matched content
const match = text.match(/pattern/);

一致とグループを繰り返す

// Iterate matches and groups
for (const m of text.matchAll(/pattern/g)) {
  console.log(m[0], m.groups);
}

TypeScript

一致するか確認

// Check if the text matches
const isMatch: boolean = /pattern/.test(text);

一致内容を取得

// Get the matched content
const match: RegExpMatchArray | null = text.match(/pattern/);

一致とグループを繰り返す

// Iterate matches and groups
for (const m of text.matchAll(/pattern/g)) {
  console.log(m[0], m.groups);
}

Python

一致するか確認

# Check if the text matches
import re
is_match = bool(re.search(r"pattern", text))

一致内容を取得

# Get the matched content
m = re.search(r"pattern", text)

一致とグループを繰り返す

# Iterate matches and groups
for m in re.finditer(r"pattern", text):
    print(m.group(0), m.groups())

Java

一致するか確認

// Check if the text matches
Pattern p = Pattern.compile("pattern");
Matcher m = p.matcher(text);
boolean isMatch = m.find();

一致内容を取得

// Get the matched content
String matched = m.group();

一致とグループを繰り返す

// Iterate matches and groups
while (m.find()) {
  System.out.println(m.group());
  System.out.println(m.groupCount());
}

C#

一致するか確認

// Check if the text matches
Regex regex = new Regex("pattern");
bool isMatch = regex.IsMatch(text);

一致内容を取得

// Get the matched content
Match match = regex.Match(text);
string matched = match.Value;

一致とグループを繰り返す

// Iterate matches and groups
foreach (Match m in regex.Matches(text)) {
  Console.WriteLine(m.Value);
  foreach (Group g in m.Groups) Console.WriteLine(g.Value);
}

Go

一致するか確認

// Check if the text matches
re := regexp.MustCompile("pattern")
isMatch := re.MatchString(text)

一致内容を取得

// Get the matched content
match := re.FindString(text)

一致とグループを繰り返す

// Iterate matches and groups
for _, m := range re.FindAllStringSubmatch(text, -1) {
  fmt.Println(m[0], m[1:])
}

PHP

一致するか確認

// Check if the text matches
$isMatch = (bool) preg_match("/pattern/", $text);

一致内容を取得

// Get the matched content
preg_match("/pattern/", $text, $match);

一致とグループを繰り返す

// Iterate matches and groups
preg_match_all("/pattern/", $text, $matches, PREG_SET_ORDER);
foreach ($matches as $m) { print_r($m); }

Ruby

一致するか確認

# Check if the text matches
is_match = !!(text =~ /pattern/)

一致内容を取得

# Get the matched content
match = text.match(/pattern/)

一致とグループを繰り返す

# Iterate matches and groups
text.scan(/pattern/) do |m|
  puts m
end

Swift

一致するか確認

// Check if the text matches
let isMatch = text.range(of: "pattern", options: .regularExpression) != nil

一致内容を取得

// Get the matched content
let range = text.range(of: "pattern", options: .regularExpression)

一致とグループを繰り返す

// Iterate matches and groups
let regex = try NSRegularExpression(pattern: "pattern")
regex.enumerateMatches(in: text, range: NSRange(location: 0, length: text.utf16.count)) { m, _, _ in
  print(m?.string ?? "")
}

Kotlin

一致するか確認

// Check if the text matches
val isMatch = Regex("pattern").containsMatchIn(text)

一致内容を取得

// Get the matched content
val match = Regex("pattern").find(text)

一致とグループを繰り返す

// Iterate matches and groups
for (m in Regex("pattern").findAll(text)) {
  println(m.value)
  println(m.groups)
}