Improve Regular Expressions (RegEx) demo (#821)

Co-authored-by: Cykyrios <cykyrios@gmail.com>
This commit is contained in:
Hugo Locurcio 2023-01-17 21:38:33 +01:00 committed by GitHub
parent 6bdf4cd06e
commit 8a6a3a8eef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 116 additions and 47 deletions

View File

@ -5,10 +5,10 @@ Can also serve as a playground for regex testing.
Language: GDScript
Renderer: GLES 2
Renderer: Compatibility
Check out this demo on the asset library: https://godotengine.org/asset-library/asset/149
## Screenshots
![Screenshot](screenshots/regex.png)
![Screenshot](screenshots/regex.webp)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -1,8 +1,9 @@
[remap]
importer="texture"
type="StreamTexture2D"
path="res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
type="CompressedTexture2D"
uid="uid://bogy8x2ym0p20"
path="res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"
metadata={
"vram_texture": false
}
@ -10,26 +11,24 @@ metadata={
[deps]
source_file="res://icon.png"
dest_files=["res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"]
dest_files=["res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/hdr_compression=1
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
process/normal_map_invert_y=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

View File

@ -6,7 +6,7 @@
; [section] ; section goes between []
; param=value ; assign values to parameters
config_version=4
config_version=5
[application]
@ -14,9 +14,16 @@ config/name="RegEx (Regular Expressions)"
config/description="A demo showing regex functionality and usage.
Can also serve as a playground for regex testing."
run/main_scene="res://regex.tscn"
config/features=PackedStringArray("4.0")
config/icon="res://icon.png"
[display]
window/stretch/mode="canvas_items"
window/stretch/aspect="expand"
[rendering]
renderer/rendering_method="gl_compatibility"
renderer/rendering_method.mobile="gl_compatibility"
environment/defaults/default_clear_color=Color(0.266667, 0.266667, 0.266667, 1)

View File

@ -3,8 +3,8 @@ extends VBoxContainer
var regex = RegEx.new()
func _ready():
$Text.set_text("They asked me \"What's going on \\\"in the manor\\\"?\"")
update_expression($Expression.text)
%Text.set_text("They asked me \"What's going on \\\"in the manor\\\"?\"")
update_expression(%Expression.text)
func update_expression(text):
@ -13,12 +13,35 @@ func update_expression(text):
func update_text():
for child in $List.get_children():
for child in %List.get_children():
child.queue_free()
if regex.is_valid():
var matches = regex.search($Text.get_text())
if matches != null:
for result in matches.get_strings():
var label = Label.new()
label.text = result
$List.add_child(label)
$HBoxContainer.modulate = Color.WHITE
var matches = regex.search_all(%Text.get_text())
if matches.size() >= 1:
# List all matches and their respective captures.
var match_number = 0
for regex_match in matches:
match_number += 1
# `match` is a reserved GDScript keyword.
var match_label = Label.new()
match_label.text = "RegEx match #%d:" % match_number
match_label.modulate = Color(0.6, 0.9, 1.0)
%List.add_child(match_label)
var capture_number = 0
for result in regex_match.get_strings():
capture_number += 1
var capture_label = Label.new()
capture_label.text = " Capture group #%d: %s" % [capture_number, result]
%List.add_child(capture_label)
else:
$HBoxContainer.modulate = Color(1, 0.2, 0.1)
var label = Label.new()
label.text = "Error: Invalid regular expression. Check if the expression is correctly escaped and terminated."
%List.add_child(label)
func _on_help_meta_clicked(_meta):
# Workaround for clickable link doing nothing when clicked.
OS.shell_open("https://regexr.com")

View File

@ -1,34 +1,74 @@
[gd_scene load_steps=2 format=2]
[gd_scene load_steps=2 format=3 uid="uid://cimp70l7c0y2p"]
[ext_resource path="res://regex.gd" type="Script" id=1]
[ext_resource type="Script" path="res://regex.gd" id="1"]
[node name="Regex" type="VBoxContainer"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = 14.0
offset_top = 20.0
offset_right = -14.0
offset_bottom = -20.0
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}
grow_horizontal = 2
grow_vertical = 2
theme_override_constants/separation = 10
script = ExtResource("1")
[node name="Expression" type="LineEdit" parent="."]
offset_right = 996.0
offset_bottom = 24.0
[node name="Help" type="RichTextLabel" parent="."]
modulate = Color(1, 1, 1, 0.752941)
custom_minimum_size = Vector2(0, 50)
layout_mode = 2
bbcode_enabled = true
text = "Regular expressions (RegEx) can be used for advanced text matching.
You can use [url=https://regexr.com]RegExr[/url] to test regular expressions online and compare results."
[node name="HBoxContainer" type="HBoxContainer" parent="."]
layout_mode = 2
theme_override_constants/separation = 10
[node name="Label" type="Label" parent="HBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 5
size_flags_stretch_ratio = 0.08
text = "RegEx"
horizontal_alignment = 2
[node name="Expression" type="LineEdit" parent="HBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 3
text = "\"((?:\\\\.|[^\"])*)\""
placeholder_text = "Regular expression to match against"
[node name="Text" type="TextEdit" parent="."]
offset_top = 28.0
offset_right = 996.0
offset_bottom = 328.0
rect_min_size = Vector2(0, 300)
[node name="HBoxContainer2" type="HBoxContainer" parent="."]
layout_mode = 2
theme_override_constants/separation = 10
[node name="List" type="VBoxContainer" parent="."]
offset_top = 332.0
offset_right = 996.0
offset_bottom = 332.0
[node name="Label" type="Label" parent="HBoxContainer2"]
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 5
size_flags_stretch_ratio = 0.08
text = "Text"
horizontal_alignment = 2
[connection signal="text_changed" from="Expression" to="." method="update_expression"]
[connection signal="text_changed" from="Text" to="." method="update_text"]
[node name="Text" type="TextEdit" parent="HBoxContainer2"]
unique_name_in_owner = true
custom_minimum_size = Vector2(0, 200)
layout_mode = 2
size_flags_horizontal = 3
placeholder_text = "Text to search in"
[node name="ScrollContainer" type="ScrollContainer" parent="."]
layout_mode = 2
size_flags_vertical = 3
[node name="List" type="VBoxContainer" parent="ScrollContainer"]
unique_name_in_owner = true
layout_mode = 2
[connection signal="meta_clicked" from="Help" to="." method="_on_help_meta_clicked"]
[connection signal="text_changed" from="HBoxContainer/Expression" to="." method="update_expression"]
[connection signal="text_changed" from="HBoxContainer2/Text" to="." method="update_text"]

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB