-
使用 compile 函数将正则表达式的字符串形式编译为一个 Pattern 对象
-
在re.complie 里面是没什么用的。r修饰符的主要作用是防止字符串被转义,但是re.complie 不会主动去转义。
-
对于一个数据量较大的数据,compile可以省去查找缓存区的,这部分时间,(实际这点时间影响不大)。
-
用于绑定到其他的可重用的名称上(很多人用他的真正原因)
s = "adfad asdfasdf asdfas asdfawef asd adsfas "
规则一:当给出的正则表达式中带有多个括号时,列表的元素为多个字符串组成的tuple,tuple中字符串个数与括号对数相同,字符串内容与每个括号内的正则表达式相对应,并且排放顺序是按括号出现的顺序。如下面两个例子。
re
Obj1 =
re
.
compile
('((\w+)(\s)(\w+))') #带
.
.
.
re
.
compile
:
编译正则表达式模式,返回一个对象。可以把常用的正则表达式编译成正则表达式对象,方便后续调用及提高效率。
re
.
compile
(
pattern
, flags=0)
pattern
指定编译时的表达式字符串
flags 编译标志位,用来修改正则表达式的匹配方式。支持
re
.
L|
re
.
M 同时匹配
flags 标志位参数:
re
.
I(
re
.
IGNO
RE
CASE) :使
.
.
.
什么是
re
模块,
re
模块有什么作用?
re
模块是
Python
提供的一个正则表达式相关的模块,主要是针对字符串进行模糊匹配,所以在字符串匹配这一功能上,
re
相当专业。
什么是模糊匹配?
之前的学习字符串内容的时候,也有进行匹配的一些方法,比如find()方法:
S = 'adnsanpnqbciqwocb'
re
t = S
.
find('san')
print(
re
t)
&
.
.
.
今天学习到
re
.
compile
()发现,search,findall 等方法都可以直接使用
pattern
,
re
.
compile
貌似没有存在的
必要
。
通过在网上搜索
re
.
compile
,发现
re
.
compile
的作用确实可有可无,因为对于search,find all等方法本身就会调用
compile
,而且当多次调用时也不需要重复编译正则表达式,而是去缓存区查找编译好的表达式。因此
re
.
compile
的作用大致有2个:
1
.
对于一个数据量较大的数据,
compile
可以省去查找缓存区的
这部分时间,(
from actionlib
.
action_client import GoalManager
import rospy
from geometry_msgs
.
msg import Twist
import actionlib
from move_base_msgs
.
msg import MoveBaseAction, MoveBaseGoal
import
re
import os
import time
def send_goal(goal_number, goal):
client = actionlib
.
SimpleActionClient('move_base', MoveBaseAction)
client
.
wait_for_server()
goal
.
target_pose
.
header
.
frame_id = "map"
goal
.
target_pose
.
header
.
stamp = rospy
.
Time
.
now()
client
.
send_goal(goal)
str_log = "Send NO
.
%s Goal !!!" % str(goal_number)
rospy
.
loginfo(str_log)
wait = client
.
wait_for_
re
sult(rospy
.
Duration
.
from_sec(60
.
0))
if not wait:
str_log = "The NO
.
%s Goal Planning Failed for some
re
asons" % str(goal_number)
rospy
.
loginfo(str_log)
else:
str_log = "The NO
.
%s Goal achieved success !!!" % str(goal_number)
rospy
.
loginfo(str_log)
def
re
ad_goal(filename):
goal = MoveBaseGoal()
file_to_
re
ad = open(filename)
index = 0
for line in file_to_
re
ad
.
re
adlines():
line = line
.
strip()
index += 1
if index == 2:
pattern
=
re
.
compile
(r"(
?
<=\[)
.
*
?
(
?
=\])")
query =
pattern
.
search(line)
listFromLine = query
.
group()
.
split(',')
goal
.
target_pose
.
pose
.
position
.
x = float(listFromLine[0])
goal
.
target_pose
.
pose
.
position
.
y = float(listFromLine[1])
if index == 3:
pattern
=
re
.
compile
(r"(
?
<=\[)
.
*
?
(
?
=\])")
query =
pattern
.
search(line)
listFromLine = query
.
group()
.
split(',')
goal
.
target_pose
.
pose
.
orientation
.
z = float(listFromLine[2])
goal
.
target_pose
.
pose
.
orientation
.
w = float(listFromLine[3])
print(goal
.
target_pose
.
pose)
re
turn goal
def pick_aruco():
os
.
system("
python
/home/robuster/beetle_ai/scripts/3pick
.
py")
def place():
os
.
system("
python
/home/robuster/beetle_ai/scripts/place_2
.
py")
def
re
ach_test1():
os
.
system("
python
/home/robuster/beetle_ai/scripts/
re
ach_place1
.
py")
def test_2():
os
.
system("
python
/home/robuster/beetle_ai/scripts/2test_2
.
py")
def test_1():
os
.
system("
python
/home/robuster/beetle_ai/scripts/2test_1
.
py")
def test():
os
.
system("
python
/home/robuster/beetle_ai/scripts/2test
.
py")
def test_3():
os
.
system("
python
/home/robuster/beetle_ai/scripts/2test3
.
py")
def back_1():
os
.
system("
python
/home/robuster/beetle_ai/scripts/4
.
py")
def forward():
print("forward
.
.
.
")
move_cmd = Twist()
pub = rospy
.
Publisher('cmd_vel', Twist, queue_size=1)
count = 15
rate = rospy
.
Rate(count) # 20hz
while count > 0:
move_cmd
.
linear
.
x = 0
.
1
pub
.
publish(move_cmd)
rate
.
sleep()
print("forward
.
.
.
")
count -= 1
def backward():
print("backward
.
.
.
")
move_cmd = Twist()
pub = rospy
.
Publisher('cmd_vel', Twist, queue_size=1)
count = 5
rate = rospy
.
Rate(count) # 20hz
while count > 0:
move_cmd
.
linear
.
x = -0
.
1
pub
.
publish(move_cmd)
rate
.
sleep()
print("forward
.
.
.
")
count -= 1
def moblie_fetch_demo():
goal1 =
re
ad_goal("goal_1
.
txt")
goal2 =
re
ad_goal("goal_2
.
txt")
# pick cube from goal1
goal_number = 1
re
ach_test1()
pick_aruco()
test()
pick_aruco()
test_1()
pick_aruco()
test_2()
pick_aruco()
test_3()
# pick cube from goal2
back_1()
re
turn "Mission Finished
.
"
simple_mobile_navigation_
re
sult = simple_mobile_navigation()
rospy
.
loginfo(simple_mobile_navigation_
re
sult)
if __name__ == '__main__':
rospy
.
init_node('send_goals_
python
', anonymous=True)
re
sult = moblie_fetch_demo()帮我分析机器人移到是那一块代码,
rospy
.
loginfo(
re
sult)
在做项目的过程中,有时候需要再一个几万行的代码里找到某个信号,并且把它拷贝出来用。信号的定义格式是相同的,但是编号不相同,如下图所示。按道理说可以利用vim的查找功能一个个找,然后一个个地手工拷贝。如果电路有修改和迭代,查找+拷贝的方式太费时费力了,所以想到用
python
来处理这种有规律的、重复性的工作。
re
.
compile
()
用正则表达式可以匹配到我想找出的信号,使用正则表达式匹配之前将
re
模块导入。
import
re
re
.
compile
()函数用于编译正则表达式,生成一个
Pattern
对象,供
import
re
pattern
=
re
.
compile
('[a-zA-Z]')
re
sult =
pattern
.
findall('as3SiOPdj#@23awe')
print (
re
sult)
['a', 's', 'S', 'i', 'O', 'P', 'd', 'j', 'a', 'w', 'e']
正则表达式是一个特殊的字符序列,能够帮助我们检查一个字符串是否与某种模式匹配。
compile
函数根据一个模式字符串和可选的标志参数生成一个正则表达式对象,该对象拥有一系列方法用于正则表达式匹配和替换。
re
.
match函数
re
.
match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。
re
.
match(
pattern
, string,
.
.
.
>>> import
re
>>>
re
.
search(r"test","hah test 123")
<_s
re
.
S
RE
_Match object; span=(4, 8), match='test'>
search()方法用于在
.
.
.