Community Patterns

1

ตรวจสอบพยัญชนะต้นตัวสะกดสระและวรรณยุกต์ไทย

Created·2026-01-22 01:36
Updated·2026-01-23 12:42
Flavor·ECMAScript (JavaScript)
ตรวจสอบพยัญชนะต้น (ต้องมี) ตรวจตัวสะกดสำหรับสระที่ต้องมี ตรวจสอบการวางสระและวรรณยุกต์ไทย หมายเหตุ การตรวจสอบตัวสะกดในภาษาไทยตรวจสอบได้ยากเพราะภาษาไทยเป็นภาษาที่เขียนติด ๆ กันไม่มีการแบ่งคำอย่างชัดเจนทำให้การอ่านภาษาไทยผู้อ่านต้องใช้ความหมายของคำในการตัดสินการอ่านแบ่งคำตามความเหมาะสมเช่นคำว่า "ตากลม" อาจอ่านเป็น "ตาก-ลม" ก็ได้ หรืออ่านเป็น "ตา-กลม"ก็ได้ ดังนั้นการเขียน Regex เพื่อทำการตรวจสอบอาจช่วยได้ระดับหนึ่ง อ่าจมีผิดบ้างถูกบ้าง แต่ก็ถือว่าเป็นเครื่องมือที่ใช้ช่วยเหลือในการตรวจสอบเพิ่มเติมได้ 80% ของความเป็นไปใด้ก็แล้วกันนะครับ หวังว่าการเขียนเพิ่มเติมส่วนนี้ จะมีประโยชน์บ้างไม่มากก็น้อย
Submitted by อธิปัตย์ ล้อวงศ์งาม

Community Library Entry

1

Regular Expression
Created·2021-03-31 15:21
Flavor·Python

r'
^(__version__)(\s?=\s?)+("(.*)")
'
Open regex in editor

Description

Python Dunder Underscore Regular Expression

Match

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r'^(__version__)(\s?=\s?)+("(.*)")'

test_str = "__version__ = \"0.1.1.1\""

matches = re.search(regex, test_str)

if matches:
    print ("Match was found at {start}-{end}: {match}".format(start = matches.start(), end = matches.end(), match = matches.group()))
    
    for groupNum in range(0, len(matches.groups())):
        groupNum = groupNum + 1
        
        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = matches.start(groupNum), end = matches.end(groupNum), group = matches.group(groupNum)))

# Note: for Python 2.7 compatibility, use ur'' to prefix the regex and u"" to prefix the test string and substitution.

Substitution

Full String Replacement

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r'^(__version__)(\s?=\s?)+("(.*)")'

test_str = "__version__ = \"0.1.1.1\""

subst = "[Replacement]"

# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 1)

if result:
    print (result)

# Note: for Python 2.7 compatibility, use ur'' to prefix the regex and u"" to prefix the test string and substitution.

Dunder Replacement

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r'(__version__)(\s?=\s?)+("(.*)")'

test_str = "__version__ = \"0.1.1.1\""

subst = "[Dunder-Replacement] \\g<3>"

# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 1)

if result:
    print (result)

# Note: for Python 2.7 compatibility, use ur'' to prefix the regex and u"" to prefix the test string and substitution.

Version Replacement

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r'(__version__)(\s?=\s?)+("(.*)")'

test_str = "__version__ = \"0.1.1.1\""

subst = " \\g<1> = [Dunder-Replacement]"

# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 1)

if result:
    print (result)

# Note: for Python 2.7 compatibility, use ur'' to prefix the regex and u"" to prefix the test string and substitution.
Submitted by Jacob B. Sanders