عبارات منظم یا Regex، یک مجموعه از الگوها و نمادهای خاصه که برای جستجو، استخراج و تحلیل الگوهای خاص در رشتهها (متنها) استفاده میشه. این الگوها میتونن الگوهای ساده مثل جستجوی یک کلمه خاص یا الگوهای پیچیدهتر مثل جستجوی ایمیلها، شماره تلفنها، آدرسهای اینترنتی و ... باشن.
<?php
$text = "Hello, World! This is a sample text.";
$pattern = "/\b\w{5}\b/"; // Matches words with exactly 5 characters
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);
?>
let text = "Hello, World! This is a sample text.";
let pattern = /\b\w{5}\b/g; // Matches words with exactly 5 characters
let matches = text.match(pattern);
console.log(matches);
import re
text = "Hello, World! This is a sample text."
pattern = r'\b\w{5}\b' # Matches words with exactly 5 characters
matches = re.findall(pattern, text)
print(matches)
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string text = "Hello, World! This is a sample text.";
string pattern = @"\b\w{5}\b"; // Matches words with exactly 5 characters
MatchCollection matches = Regex.Matches(text, pattern);
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
}
}
}
در این مثالها یک الگو Regex برای پیدا کردن کلماتی با طول ۵ حرف در یک متن مشخص، تعریف شده. همچنین میتونید پترن های کاستوم شده تری با توجه به نیازتون بنویسید
#regex #language
@CodeModule
Please open Telegram to view this post
VIEW IN TELEGRAM
⚡9🔥3😁1💔1