最も簡易的と思われる
正規表現の使用例
VC++
#include <stdio.h>
#include <regex>
void main()
{
bool IsMatch = std::regex_search("123aaa456",std::regex("[0-9]+$"));
puts(IsMatch ? "true" : "false"); //結果はtrue
std::cmatch matcher;
std::regex_search("123aaa456", matcher, std::regex("[0-9]+"));
if (matcher.size() > 0) {
puts(matcher.begin()->str().c_str()); //結果は123
}
std::regex MatchPattern("^([0-9]+)a+([0-9]+)$");
std::string ReplaceResult = std::regex_replace("123aaa456",MatchPattern, "$1$2");
puts(ReplaceResult.c_str()); //結果は123456
}
C#
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
bool IsMatch = Regex.IsMatch("123aaa456", "[0-9]+$");
string num1 = Regex.Match("123aaa456", "^[0-9]+").Value; //結果は、123
string num2 = Regex.Replace("123aaa456", "^([0-9]+)a+([0-9]+)$", "$1$2"); //結果は、123456
}
}
C#のサンプル集 5-2 括弧のキャプチャ内容の取得
.NETの正規表現のドキュメント
VB.NET
imports System.Text.RegularExpressions
Dim IsMatch As Boolean = Regex.IsMatch("123aaa456","[0-9]+$")
Dim num1 As String = Regex.Match("123aaa456","^[0-9]+").Value
Dim num2 As String = Regex.Replace("123aaa456","^([0-9]+)a+([0-9]+)$","$1$2")
.NETの正規表現のドキュメント
Oracle
select case when RegExp_Like(Val,'[0-9]+$')
then 1 else 0 end as IsMatch,
RegExp_Substr(Val,'^([0-9]+)') as num1,
RegExp_Replace(Val,'^([0-9]+)a+([0-9]+)$','\1\2') as num2
from (select '123aaa456' as Val from dual);
Oracleの正規表現のドキュメント
Java
boolean IsMatch = "123aaa456".matches("(?m)[0-9]+$");
String num = "123aaa456".replaceAll("(?m)^([0-9]+)a+([0-9]+)$","$1$2");
if ("abcde".matches("^.*(..)\\1.*$")) continue;
Ruby
ConstRegex = Regexp.new('^AB|ABBB|ABC')
hairetu = Array.new
hairetu.push('ABC')
hairetu.push('ABBB')
puts "Pattern #{ConstRegex.source}"
for i in (0..hairetu.length-1)
willOut = "Line#{i+1} #{hairetu[i]} "
work = ConstRegex.match(hairetu[i]).to_a[0]
if work then
willOut += 'matchs ' + work
else
willOut += 'no match'
end
puts willOut
end
#実行結果
Pattern ^AB|ABBB|ABC
Line1 ABC matchs AB
Line2 ABBB matchs AB