Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

week nine homework #97

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jingdai/Py100-2017q1
Submodule Py100-2017q1 added at d71163
27 changes: 27 additions & 0 deletions jingdai/RegularExpression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import re

string='please help me to find numbers like ABC-123-4567#, YBN-345-8970*'

regex=re.compile(r'\w\w\w-\d\d\d-\d\d\d\d\S')

result= regex.search(string)

print(result.group())

# try group the pattern individually

regex=re.compile(r'(\w\w\w)-(\d\d\d)-(\d\d\d\d\S)') # r for raw_string

result= regex.search(string)

print(result.group(3))

print(result.groups()) # return a tuple

# search function only returns first occurrence , how to return all occurrence?

regex=re.compile(r'\w\w\w-\d\d\d-\d\d\d\d\S')
results= regex.findall(string)
print(results)


230 changes: 230 additions & 0 deletions jingdai/Regular_Expression.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import re"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"string='please help me to find numbers like ABC-123-4567#, YBN-345-8970*'"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"regex=re.compile(r'\\w\\w\\w-\\d\\d\\d-\\d\\d\\d\\d\\S')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\\w for letter, \\d for number, \\S for any characters that is not a space, tab or newline; r for raw_string"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"result= regex.search(string)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ABC-123-4567#\n"
]
}
],
"source": [
"print(result.group())"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# try group the pattern individually"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"regex=re.compile(r'(\\w\\w\\w)-(\\d\\d\\d)-(\\d\\d\\d\\d\\S)') # r for raw_string"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"result= regex.search(string)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4567#\n"
]
}
],
"source": [
"print(result.group(3))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('ABC', '123', '4567#')\n"
]
}
],
"source": [
"print(result.groups()) # return a tuple"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#search function only returns first occurrence , how to return all occurrence?"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"regex=re.compile(r'\\w\\w\\w-\\d\\d\\d-\\d\\d\\d\\d\\S')"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"results= regex.findall(string)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['ABC-123-4567#', 'YBN-345-8970*']\n"
]
}
],
"source": [
"print(results)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"note: .findall() returns a list"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading