# Regular expressions

Simple matching:

```ruby
var str = "I am a string"
if (str =~ /string$/) {
    print "Ends with 'string'\n"
}
```

Global matching:

```ruby
var str = <<'EOF'
    x:Foo
    y:Bar
EOF
 
while (var m = str=~/(\w+):(\S+)/g) {
    say "#{m[0]} -> #{m[1]}"
}
```

Substitutions:

```ruby
var str = "I am a string"
 
# Substitute something mached by a regex
str.sub!(/ a /, ' another ')   # "I am a string" => "I am another string"
 
# Remove something matched by a regex
str -= / \Kanother /i          # "I am another string" => "I am string"
 
# Global subtitution with a block
str = str.gsub(/(\w+)/, {|s1| 'x' * s1.len})  # globaly replace any word with 'xxx'
 
say str     # prints: 'x xx xxxxxx'
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://trizen.gitbook.io/sidef-lang/programming_tasks/r/regular_expressions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
