Update.
This commit is contained in:
@@ -0,0 +1,337 @@
|
||||
<!DOCTYPE html>
|
||||
<!-- retoor <retoor@molodetz.nl> -->
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Using Regular Expressions - Wren-CLI Manual</title>
|
||||
<link rel="stylesheet" href="../css/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<button class="mobile-menu-toggle">Menu</button>
|
||||
<div class="container">
|
||||
<aside class="sidebar">
|
||||
<div class="sidebar-header">
|
||||
<h1><a href="../index.html">Wren-CLI</a></h1>
|
||||
<div class="version">v0.4.0</div>
|
||||
</div>
|
||||
<nav class="sidebar-nav">
|
||||
<div class="section">
|
||||
<span class="section-title">Getting Started</span>
|
||||
<ul>
|
||||
<li><a href="../getting-started/index.html">Overview</a></li>
|
||||
<li><a href="../getting-started/installation.html">Installation</a></li>
|
||||
<li><a href="../getting-started/first-script.html">First Script</a></li>
|
||||
<li><a href="../getting-started/repl.html">Using the REPL</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="section">
|
||||
<span class="section-title">Language</span>
|
||||
<ul>
|
||||
<li><a href="../language/index.html">Syntax Overview</a></li>
|
||||
<li><a href="../language/classes.html">Classes</a></li>
|
||||
<li><a href="../language/methods.html">Methods</a></li>
|
||||
<li><a href="../language/control-flow.html">Control Flow</a></li>
|
||||
<li><a href="../language/fibers.html">Fibers</a></li>
|
||||
<li><a href="../language/modules.html">Modules</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="section">
|
||||
<span class="section-title">API Reference</span>
|
||||
<ul>
|
||||
<li><a href="../api/index.html">Overview</a></li>
|
||||
<li><a href="../api/http.html">http</a></li>
|
||||
<li><a href="../api/websocket.html">websocket</a></li>
|
||||
<li><a href="../api/tls.html">tls</a></li>
|
||||
<li><a href="../api/net.html">net</a></li>
|
||||
<li><a href="../api/dns.html">dns</a></li>
|
||||
<li><a href="../api/json.html">json</a></li>
|
||||
<li><a href="../api/base64.html">base64</a></li>
|
||||
<li><a href="../api/regex.html">regex</a></li>
|
||||
<li><a href="../api/jinja.html">jinja</a></li>
|
||||
<li><a href="../api/crypto.html">crypto</a></li>
|
||||
<li><a href="../api/os.html">os</a></li>
|
||||
<li><a href="../api/env.html">env</a></li>
|
||||
<li><a href="../api/signal.html">signal</a></li>
|
||||
<li><a href="../api/subprocess.html">subprocess</a></li>
|
||||
<li><a href="../api/sqlite.html">sqlite</a></li>
|
||||
<li><a href="../api/datetime.html">datetime</a></li>
|
||||
<li><a href="../api/timer.html">timer</a></li>
|
||||
<li><a href="../api/io.html">io</a></li>
|
||||
<li><a href="../api/scheduler.html">scheduler</a></li>
|
||||
<li><a href="../api/math.html">math</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="section">
|
||||
<span class="section-title">Tutorials</span>
|
||||
<ul>
|
||||
<li><a href="../tutorials/index.html">Tutorial List</a></li>
|
||||
<li><a href="../tutorials/http-client.html">HTTP Client</a></li>
|
||||
<li><a href="../tutorials/websocket-chat.html">WebSocket Chat</a></li>
|
||||
<li><a href="../tutorials/database-app.html">Database App</a></li>
|
||||
<li><a href="../tutorials/template-rendering.html">Template Rendering</a></li>
|
||||
<li><a href="../tutorials/cli-tool.html">CLI Tool</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="section">
|
||||
<span class="section-title">How-To Guides</span>
|
||||
<ul>
|
||||
<li><a href="index.html">How-To List</a></li>
|
||||
<li><a href="http-requests.html">HTTP Requests</a></li>
|
||||
<li><a href="json-parsing.html">JSON Parsing</a></li>
|
||||
<li><a href="regex-patterns.html" class="active">Regex Patterns</a></li>
|
||||
<li><a href="file-operations.html">File Operations</a></li>
|
||||
<li><a href="async-operations.html">Async Operations</a></li>
|
||||
<li><a href="error-handling.html">Error Handling</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</nav>
|
||||
</aside>
|
||||
<main class="content">
|
||||
<nav class="breadcrumb">
|
||||
<a href="../index.html">Home</a>
|
||||
<span class="separator">/</span>
|
||||
<a href="index.html">How-To Guides</a>
|
||||
<span class="separator">/</span>
|
||||
<span>Regex Patterns</span>
|
||||
</nav>
|
||||
|
||||
<article>
|
||||
<h1>Using Regular Expressions</h1>
|
||||
|
||||
<h2>Test if String Matches Pattern</h2>
|
||||
<pre><code>import "regex" for Regex
|
||||
|
||||
var pattern = Regex.new("^hello")
|
||||
|
||||
System.print(pattern.test("hello world")) // true
|
||||
System.print(pattern.test("say hello")) // false</code></pre>
|
||||
|
||||
<h2>Find First Match</h2>
|
||||
<pre><code>import "regex" for Regex
|
||||
|
||||
var pattern = Regex.new("\\d+")
|
||||
var match = pattern.match("Order 12345 shipped")
|
||||
|
||||
if (match) {
|
||||
System.print(match.text) // 12345
|
||||
System.print(match.start) // 6
|
||||
System.print(match.end) // 11
|
||||
}</code></pre>
|
||||
|
||||
<h2>Find All Matches</h2>
|
||||
<pre><code>import "regex" for Regex
|
||||
|
||||
var pattern = Regex.new("\\d+")
|
||||
var matches = pattern.matchAll("Items: 10, 20, 30")
|
||||
|
||||
for (match in matches) {
|
||||
System.print(match.text)
|
||||
}
|
||||
// 10
|
||||
// 20
|
||||
// 30</code></pre>
|
||||
|
||||
<h2>Capture Groups</h2>
|
||||
<pre><code>import "regex" for Regex
|
||||
|
||||
var pattern = Regex.new("(\\w+)@(\\w+\\.\\w+)")
|
||||
var match = pattern.match("Contact: alice@example.com")
|
||||
|
||||
if (match) {
|
||||
System.print(match.group(0)) // alice@example.com
|
||||
System.print(match.group(1)) // alice
|
||||
System.print(match.group(2)) // example.com
|
||||
}</code></pre>
|
||||
|
||||
<h2>Replace Matches</h2>
|
||||
<pre><code>import "regex" for Regex
|
||||
|
||||
var pattern = Regex.new("\\bcat\\b")
|
||||
var result = pattern.replace("The cat sat on the cat mat", "dog")
|
||||
|
||||
System.print(result) // The dog sat on the dog mat</code></pre>
|
||||
|
||||
<h2>Replace with Callback</h2>
|
||||
<pre><code>import "regex" for Regex
|
||||
|
||||
var pattern = Regex.new("\\d+")
|
||||
var result = pattern.replace("a1b2c3", Fn.new { |match|
|
||||
return "[%(match.text)]"
|
||||
})
|
||||
|
||||
System.print(result) // a[1]b[2]c[3]</code></pre>
|
||||
|
||||
<h2>Split String</h2>
|
||||
<pre><code>import "regex" for Regex
|
||||
|
||||
var pattern = Regex.new("[,;\\s]+")
|
||||
var parts = pattern.split("apple, banana; cherry date")
|
||||
|
||||
for (part in parts) {
|
||||
System.print(part)
|
||||
}
|
||||
// apple
|
||||
// banana
|
||||
// cherry
|
||||
// date</code></pre>
|
||||
|
||||
<h2>Case Insensitive Matching</h2>
|
||||
<pre><code>import "regex" for Regex
|
||||
|
||||
var pattern = Regex.new("hello", "i")
|
||||
|
||||
System.print(pattern.test("Hello World")) // true
|
||||
System.print(pattern.test("HELLO")) // true</code></pre>
|
||||
|
||||
<h2>Multiline Matching</h2>
|
||||
<pre><code>import "regex" for Regex
|
||||
|
||||
var text = "Line 1\nLine 2\nLine 3"
|
||||
var pattern = Regex.new("^Line", "m")
|
||||
|
||||
var matches = pattern.matchAll(text)
|
||||
System.print(matches.count) // 3</code></pre>
|
||||
|
||||
<h2>Common Patterns</h2>
|
||||
|
||||
<h3>Validate Email</h3>
|
||||
<pre><code>import "regex" for Regex
|
||||
|
||||
var emailPattern = Regex.new("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$")
|
||||
|
||||
System.print(emailPattern.test("user@example.com")) // true
|
||||
System.print(emailPattern.test("invalid-email")) // false</code></pre>
|
||||
|
||||
<h3>Validate URL</h3>
|
||||
<pre><code>import "regex" for Regex
|
||||
|
||||
var urlPattern = Regex.new("^https?://[a-zA-Z0-9.-]+(/.*)?$")
|
||||
|
||||
System.print(urlPattern.test("https://example.com")) // true
|
||||
System.print(urlPattern.test("http://example.com/path")) // true
|
||||
System.print(urlPattern.test("ftp://invalid")) // false</code></pre>
|
||||
|
||||
<h3>Validate Phone Number</h3>
|
||||
<pre><code>import "regex" for Regex
|
||||
|
||||
var phonePattern = Regex.new("^\\+?\\d{1,3}[-.\\s]?\\(?\\d{3}\\)?[-.\\s]?\\d{3}[-.\\s]?\\d{4}$")
|
||||
|
||||
System.print(phonePattern.test("+1-555-123-4567")) // true
|
||||
System.print(phonePattern.test("(555) 123-4567")) // true</code></pre>
|
||||
|
||||
<h3>Extract Numbers</h3>
|
||||
<pre><code>import "regex" for Regex
|
||||
|
||||
var numberPattern = Regex.new("-?\\d+\\.?\\d*")
|
||||
var text = "Temperature: -5.5 to 32.0 degrees"
|
||||
|
||||
var matches = numberPattern.matchAll(text)
|
||||
for (match in matches) {
|
||||
System.print(match.text)
|
||||
}
|
||||
// -5.5
|
||||
// 32.0</code></pre>
|
||||
|
||||
<h3>Extract Hashtags</h3>
|
||||
<pre><code>import "regex" for Regex
|
||||
|
||||
var hashtagPattern = Regex.new("#\\w+")
|
||||
var text = "Check out #wren and #programming!"
|
||||
|
||||
var matches = hashtagPattern.matchAll(text)
|
||||
for (match in matches) {
|
||||
System.print(match.text)
|
||||
}
|
||||
// #wren
|
||||
// #programming</code></pre>
|
||||
|
||||
<h3>Remove HTML Tags</h3>
|
||||
<pre><code>import "regex" for Regex
|
||||
|
||||
var tagPattern = Regex.new("<[^>]+>")
|
||||
var html = "<p>Hello <b>World</b>!</p>"
|
||||
|
||||
var text = tagPattern.replace(html, "")
|
||||
System.print(text) // Hello World!</code></pre>
|
||||
|
||||
<h3>Validate Password Strength</h3>
|
||||
<pre><code>import "regex" for Regex
|
||||
|
||||
var hasUpper = Regex.new("[A-Z]")
|
||||
var hasLower = Regex.new("[a-z]")
|
||||
var hasDigit = Regex.new("\\d")
|
||||
var hasSpecial = Regex.new("[!@#$%^&*]")
|
||||
var minLength = 8
|
||||
|
||||
var validatePassword = Fn.new { |password|
|
||||
if (password.count < minLength) return false
|
||||
if (!hasUpper.test(password)) return false
|
||||
if (!hasLower.test(password)) return false
|
||||
if (!hasDigit.test(password)) return false
|
||||
if (!hasSpecial.test(password)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
System.print(validatePassword.call("Weak")) // false
|
||||
System.print(validatePassword.call("Strong@Pass1")) // true</code></pre>
|
||||
|
||||
<h3>Parse Log Lines</h3>
|
||||
<pre><code>import "regex" for Regex
|
||||
|
||||
var logPattern = Regex.new("\\[(\\d{4}-\\d{2}-\\d{2})\\]\\s+(\\w+):\\s+(.+)")
|
||||
|
||||
var line = "[2024-01-15] ERROR: Connection failed"
|
||||
var match = logPattern.match(line)
|
||||
|
||||
if (match) {
|
||||
System.print("Date: %(match.group(1))") // 2024-01-15
|
||||
System.print("Level: %(match.group(2))") // ERROR
|
||||
System.print("Message: %(match.group(3))") // Connection failed
|
||||
}</code></pre>
|
||||
|
||||
<h3>Validate IP Address</h3>
|
||||
<pre><code>import "regex" for Regex
|
||||
|
||||
var ipPattern = Regex.new("^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$")
|
||||
|
||||
System.print(ipPattern.test("192.168.1.1")) // true
|
||||
System.print(ipPattern.test("256.1.1.1")) // false
|
||||
System.print(ipPattern.test("10.0.0.255")) // true</code></pre>
|
||||
|
||||
<h2>Pattern Syntax Quick Reference</h2>
|
||||
|
||||
<table>
|
||||
<tr><th>Pattern</th><th>Description</th></tr>
|
||||
<tr><td><code>.</code></td><td>Any character except newline</td></tr>
|
||||
<tr><td><code>\\d</code></td><td>Digit (0-9)</td></tr>
|
||||
<tr><td><code>\\w</code></td><td>Word character (a-z, A-Z, 0-9, _)</td></tr>
|
||||
<tr><td><code>\\s</code></td><td>Whitespace</td></tr>
|
||||
<tr><td><code>^</code></td><td>Start of string/line</td></tr>
|
||||
<tr><td><code>$</code></td><td>End of string/line</td></tr>
|
||||
<tr><td><code>*</code></td><td>Zero or more</td></tr>
|
||||
<tr><td><code>+</code></td><td>One or more</td></tr>
|
||||
<tr><td><code>?</code></td><td>Zero or one</td></tr>
|
||||
<tr><td><code>{n,m}</code></td><td>Between n and m times</td></tr>
|
||||
<tr><td><code>[abc]</code></td><td>Character class</td></tr>
|
||||
<tr><td><code>[^abc]</code></td><td>Negated character class</td></tr>
|
||||
<tr><td><code>(group)</code></td><td>Capture group</td></tr>
|
||||
<tr><td><code>a|b</code></td><td>Alternation (a or b)</td></tr>
|
||||
<tr><td><code>\\b</code></td><td>Word boundary</td></tr>
|
||||
</table>
|
||||
|
||||
<div class="admonition tip">
|
||||
<div class="admonition-title">See Also</div>
|
||||
<p>For full API documentation, see the <a href="../api/regex.html">Regex module reference</a>.</p>
|
||||
</div>
|
||||
</article>
|
||||
|
||||
<footer class="page-footer">
|
||||
<a href="json-parsing.html" class="prev">JSON Parsing</a>
|
||||
<a href="file-operations.html" class="next">File Operations</a>
|
||||
</footer>
|
||||
</main>
|
||||
</div>
|
||||
<script src="../js/main.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user