Recent Posts

Create Search Form with PHP

GOAL

Today’s goal is to create search form to find the name from the database.

Environment

Windows10
XAMPP 7.4.10

Method

This is an adjusted version of the code in “Access MySQL with PHP in XAMPP“. So please check the article first.

Search form

Use post method and the form is named “word” as below.

<form action="" method="POST">
    <label>Name:</label>
    <input type="text" name="word" /> <input type="submit" value="Search" />
</form>
(more…)

How Get Value With Format in Python

GOAL

Today’s goal is to check if the string follows the format and to get the value according to the format. The following is an example.

# the format is "My name is <NAME>, the phone number is <NUMBER>" (<NUMBER> is separated by 2 '-')

str1 = "My name is Nako, the phone number is 123-456-7890"
#str1 follows the format, <NAME> is "Nako" and <NUMBER> is "1234567890" in this case

str2 = "I am Nako, the phone number is 1234567890"
# str2 doesn't follow the format

Environment

Python 3.8.7

Method

Use regular expression

Create regular expression object with re.compile(pattern, flags=0). It can be used to check if the string matches the pattern or not by using Pattern.match(string) function.

# coding: utf-8
import re

str1 = "My name is Nako, the phone number is 123-456-7890"
str2 = "I am Nako, the phone number is 1234567890"

prog = re.compile("My name is [A-Za-z]+, the phone number is [0-9]+-[0-9]+-[0-9]+$")

print(prog.match(str1))
# output => <re.Match object; span=(0, 49), match='My name is Nako, the phone number is 123-456-7890>
print(prog.match(str2))
# output => None

You can get the value by grouping the regular expression using (). The value of each group can be get with Match.group([group1, …])

gourp(0) is the entire match and group(n) is the match for n-th group.

# coding: utf-8
import re

str1 = "My name is Nako, the phone number is 123-456-7890"

prog = re.compile("My name is ([A-Za-z]+), the phone number is ([0-9]+-[0-9]+-[0-9]+)$")

match = prog.match(str1)
if match:
    print(match.group(0)) # output => My name is Nako, the phone number is 123-456-7890
    print(match.group(1)) # output => Nako
    print(match.group(2)) # output => 123-456-7890
    print(match.group(2).replace('-', '')) # output => 1234567890

Postscript

$ as the end of the string

You should use $ that matches the end of the string because Pattern.match(string) finds zero or more characters at the beginning of string match.

fullmatch method

If you don’t use $, you should use Pattern.fullmatch(string) instead of Pattern.match(string).

str1 = "My name is Nako, the phone number is 123-456-7890ABCDE"
prog = re.compile("My name is ([A-Za-z]+), the phone number is ([0-9]+-[0-9]+-[0-9]+)")

print(prog.match(str1))
# output => <re.Match object; span=(0, 49), match='My name is Nako, the phone number is 123-456-7890>

print(prog.fullmatch(str1))
# output => None

pattern with length limitation

The length of the input can be limited by {} in regular expression.

str1 = "My name is Nako, the phone number is 123-456-78900000"
prog = re.compile("My name is ([A-Za-z]+), the phone number is ([0-9]{3}-[0-9]{3}-[0-9]{4})$")

print(prog.match(str1))
# output => None

[AttributeError] module ‘tensorflow’ has no attribute ‘Session’

When I was trying the exercise of TensorFlow Programming Concepts, an error “AttributeError module ‘tensorflow’ has no attribute ‘Session'” occurred.

Error Detail

The code run in Google Colaboratory is the following.Generic HighlightingEnlighterJS Syntax Highlighter

This image has an empty alt attribute; its file name is 38dbab30e4232a9115cd0f972a88d428.png
An attribute error

Cause

tf.Session can be used in tensorflow 1.x version, while it cannot be used in 2.x version.

(more…)

Categories

AfterEffects Algorithm Artificial Intelligence Blender C++ Computer Graphics Computer Science Daily Life DataAnalytics Event Game ImageProcessing JavaScript Kotlin mathematics Maya PHP Python SoftwareEngineering Tips Today's paper Tools TroubleShooting Unity Visual Sudio Web Windows WordPress 未分類