Regex Characters

Regular expressions (regex) in programming are patterns used to match and manipulate text. They are powerful tools for searching, validating, and extracting specific patterns from strings. Common use cases for regex include form validation, text parsing, data extraction, and search and replace operations. Regex allows for flexible and efficient handling of textual data, enabling developers to perform complex text processing tasks with relatively simple patterns. However, regex can be challenging to understand and write correctly, so proper testing and documentation are essential for ensuring accurate behavior.

Select Languages

Examples

C

// no built in implementation

C#

using System.Text.RegularExpressions;

string pattern = @"\W*\w*";
Regex rg = new Regex(pattern);

string phrase = "##I am legend##";
match match = rg.match(phrase);

Console.WriteLine(match);

C++

#include <iostream>
#include <regex> 
#include <bits/stdc++.h>
using namespace std;

string phrase = "##I am legend##";
regex pattern ("#+[a-zA-Z]+");
smatch result; 

regex_search(phrase, result, pattern);
cout << result.str(0) << endl;

Go

import (
  "fmt"
  "regexp"
)

// use back qoutes in pattern
pattern := regexp.MustCompile(`#+\w+`);
phrase := "##I am legend##"
result := pattern.FindString(phrase)
 
fmt.Println(result)

Java

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

Pattern pattern = Pattern.compile(
  "(#+\\w?)",
  Pattern.CASE_INSENSITIVE
);

String phrase = "##I am legend##";
Matcher match = pattern.matcher(phrase);
String result = match.find() ?
  match.group(1) : "";

System.out.println(result);

JavaScript

const pattern = /\#+\w?/g;
const phrase = "##I am legend##";
const result = phrase.match(pattern);

console.log(result[0]);

Kotlin

val pattern = Regex("#+\\w+");
val phrase = "##I am legend##";

val match = pattern.find(phrase);
println(match?.value);

MatLab

phrase = "##I am legend##";
pattern = regexpPattern('\#+[a-zA-Z]');
result = extract(phrase, pattern);

% only first result
disp(result(1));

PHP

$pattern = "/#+\w+/";
$phrase = "##I am Legend##";
preg_match($pattern, $phrase, $matches);

echo $matches[0];

Python

import re

pattern = '#+\w?'
phrase = '##I am legend##'
result = re.search(pattern, phrase)

print(result[0])

R

phrase <- "##I am legend##"
pattern <- "\\#+\\w+"
rg <- regexpr(pattern, phrase)

result <- regmatches(phrase, rg)
print(result)

Ruby

phrase = "##I am legend##"
pattern = /\#+\w+/
result = pattern.match(phrase)

puts result;

Rust

// no built in implementation
// use crates and external regex library

Scala

import scala.util.matching.Regex

val phrase = "##I am legend##";
val pattern: Regex = "#+[a-zA-Z]+".r;
val result = pattern.findFirstMatchIn(phrase);

print(result);

Swift

var phrase = "##I am legend##";
var pattern = try Regex("#+\\w+");

if let result = phrase.firstMatch(of: pattern) {
  print(result.0);
}

TypeScript

const pattern = /\#+\w?/g;
const phrase:string = "##I am legend##";
const result:RegExpMatchArray | null = 
  phrase.match(pattern);

console.log(result?.[0]);

Copyright 2025. All Rights Reserved. IronCodeMan.