-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve the explanation about backtick templates #5819
base: master
Are you sure you want to change the base?
Changes from 5 commits
a9e5bb4
5bf42cb
73ef2f3
61a3f56
c61fad0
dc37276
0c7b30a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
heshanpadmasiri marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import ballerina/io; | ||
|
||
public function main() { | ||
string:RegExp pat1 = re `a+`; | ||
string a0 = ""; | ||
io:println(pat1.isFullMatch(a0)); | ||
string a1 = "a"; | ||
io:println(pat1.isFullMatch(a1)); | ||
string a2 = "aa"; | ||
io:println(pat1.isFullMatch(a2)); | ||
|
||
string:RegExp pat2 = re `[a-z]+`; | ||
string b = "bb"; | ||
io:println(pat2.isFullMatch(b)); | ||
string b1 = "bb1"; | ||
io:println(pat2.isFullMatch(b1)); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Regex templates | ||
|
||
You can use template expressions with `re` tag to create regular expressions. Ballerina regular expressions are based on the ECMAScript 2022 in unicode mode. | ||
|
||
+ [Regular Expressions](https://ballerina.io/learn/advanced-general-purpose-language-features/#regular-expressions) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
description: This BBE demonstrates regex templates in Ballerina. | ||
keywords: ballerina, ballerina by example, bbe, regex templates, regex |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
$ bal run regex_template.bal | ||
false | ||
true | ||
true | ||
true | ||
false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import ballerina/io; | ||
|
||
public function main() { | ||
// Note first \n is treated as is. | ||
heshanpadmasiri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
string s1 = string `line one \n rest of line 1 ${"\n"} second line`; | ||
io:println(s1); | ||
|
||
// You can use interpolations to add ` and $ characters. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to emphasize the characters using backticks. Need to check if/how this can be done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I know we can't use markdown-ish formatting in side source code, they are rendered as is (example). I didn't add backticks around
heshanpadmasiri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
string s2 = string `Backtick: ${"`"} Dollar: ${"$"}`; | ||
io:println(s2); | ||
|
||
// You can nest template expressions. | ||
heshanpadmasiri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
string s3 = string `outer ${string `inner ${5} inner rest`} rest`; | ||
io:println(s3); | ||
|
||
// You can use an empty string interpolation to break a template expression | ||
// across multiple lines | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or break at interpolations. Let's add examples with both. Also need a fullstop at the end. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't fully understand this. Do you mean
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant breaking at a normal interpolation like string user = "John";
string s4 = string `Hello World, from ${
user}!` |
||
string s4 = string `prefix ${ | ||
""}suffix`; | ||
io:println(s4); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# String templates | ||
|
||
A string template is a template expression that can be used to create a string literal. It consists of a `string` tag and a sequence of characters interleaved with interpolations (in the form `${expression}`). Each interpolation must be a subtype of `boolean|int|float|decimal|string`. Every character not a part of the interpolation is interpreted as is to form a sequence of string literals broken at interpolations. This means if you want to add any escape characters you must add them as interpolations (for example `${"\n"}`). Every interpolation is converted to a string using the `toString` method and concatenated with the other string literals to form a single string literal. | ||
heshanpadmasiri marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
description: This BBE demonstrates string templates in Ballerina. | ||
keywords: ballerina, ballerina by example, bbe, string templates |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
$ bal run string_templates.bal | ||
line one \n rest of line 1 | ||
second line | ||
Backtick: ` Dollar: $ | ||
outer inner 5 inner rest rest | ||
prefix suffix |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we need to handle redirection for this. cc @sm1990