Python用正则表达式匹配ABAC和AABB的词语

正则表达式前加r是为了不转义反斜杠后的字符,\3代表此处重复出现子模式3

from re import findall
text = '''行尸走肉、金蝉脱壳、百里挑一、金玉满堂、
背水一战、霸王别姬、天上人间、不吐不快、海阔天空、
情非得已、满腹经纶、兵临城下、春暖花开、插翅难逃、
黄道吉日、天下无双、偷天换日、两小无猜、卧虎藏龙、
珠光宝气、簪缨世族、花花公子、绘声绘影、国色天香、
相亲相爱、八仙过海、金玉良缘、掌上明珠、皆大欢喜、
浩浩荡荡、平平安安、秀秀气气、斯斯文文、高高兴兴'''
print("全部词语: ", text)
pattern = r'(((.).\3.)|((.)\5(.)\6))'
print("匹配ABAC和AABB的词语:", list(i[0] for i in findall(pattern, text)))

在这里插入图片描述

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
ABAC(Attribute-Based Access Control)是一种基于属性的访问控制,可以根据用户的属性来控制对资源的访问。在Python实现ABAC,可以使用PyABAC库。 首先,需要定义一些实体和属性。比如,我们定义一个用户实体和一个文件资源实体,用户实体拥有一个role属性,文件资源实体拥有一个owner属性。 ```python from pyabac import Policy, AccessRequest, Action, Attribute, Access # Define entities and attributes class User: def __init__(self, role): self.role = role class FileResource: def __init__(self, owner): self.owner = owner # Define policies policy = Policy( "file access policy", { "attributes": { "role": Attribute(str), "owner": Attribute(str), }, "rules": { "allow owner to read": { "target": { "entity": "file", "attribute": "owner", }, "effect": "permit", "condition": { "equals": { "attribute": "user.role", "value": "owner", }, }, "actions": [ "read", ], }, "allow owner to write": { "target": { "entity": "file", "attribute": "owner", }, "effect": "permit", "condition": { "equals": { "attribute": "user.role", "value": "owner", }, }, "actions": [ "write", ], }, "deny others": { "target": { "entity": "file", }, "effect": "deny", "condition": { "not": { "equals": { "attribute": "user.role", "value": "owner", }, }, }, "actions": [ "read", "write", ], }, }, }, ) ``` 然后,我们可以创建一个AccessRequest对象来请求访问资源。AccessRequest对象包含了用户实体和文件资源实体的属性。 ```python # Create access request user = User("owner") file = FileResource("owner") access_request = AccessRequest( user=user, resource=file, action=Action.READ, ) ``` 最后,我们可以使用Policy对象来判断用户是否有访问资源的权限。 ```python # Check access access = policy.evaluate(access_request) if access == Access.ALLOW: print("Access granted") else: print("Access denied") ``` 完整代码示例: ```python from pyabac import Policy, AccessRequest, Action, Attribute, Access # Define entities and attributes class User: def __init__(self, role): self.role = role class FileResource: def __init__(self, owner): self.owner = owner # Define policies policy = Policy( "file access policy", { "attributes": { "role": Attribute(str), "owner": Attribute(str), }, "rules": { "allow owner to read": { "target": { "entity": "file", "attribute": "owner", }, "effect": "permit", "condition": { "equals": { "attribute": "user.role", "value": "owner", }, }, "actions": [ "read", ], }, "allow owner to write": { "target": { "entity": "file", "attribute": "owner", }, "effect": "permit", "condition": { "equals": { "attribute": "user.role", "value": "owner", }, }, "actions": [ "write", ], }, "deny others": { "target": { "entity": "file", }, "effect": "deny", "condition": { "not": { "equals": { "attribute": "user.role", "value": "owner", }, }, }, "actions": [ "read", "write", ], }, }, }, ) # Create access request user = User("owner") file = FileResource("owner") access_request = AccessRequest( user=user, resource=file, action=Action.READ, ) # Check access access = policy.evaluate(access_request) if access == Access.ALLOW: print("Access granted") else: print("Access denied") ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值