简体   繁体   English

如何使用正则表达式获取某些字符之前和之后的所有文本

[英]How to get all the text before and after some characters using regular expressions

How can I get some characters from an url, using regular expressions for example:如何使用正则表达式从 url 中获取一些字符,例如:

https://www.facebook.com/aaaaaa/posts/123456789/sdd https://www.facebook.com/aaaaaa/posts/123456789/sdd

How can I get the characters aaaa and 123456789 that are before and after /posts/ ?如何获取/posts/之前和之后的字符aaaa123456789

import re

url = "https://www.facebook.com/aaaaaa/posts/123456789/sdd"

out = re.match('.+/(.+)/posts/(.+)/', url).groups()

print(out)

# Output:

('aaaaaa', '123456789')
url = "https://www.facebook.com/aaaaaa/posts/123456789/sdd"

to get 123456789 :得到123456789

url.split('/')[5]

for aaaaaaaaaaaa

url.split('/')[3]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在匹配后和正则表达式中的关键字之前隐藏文本? - How to hide text after matching and before keyword in regular expressions? 如何使用正则表达式去除 Python 中带有特殊字符的单词? - How to get rid of words with special characters in Python using regular expressions? 如何在python中使用正则表达式搜索单词然后替换文本? - How to search for a word and then replace text after it using regular expressions in python? 如何使用正则表达式迭代字符? - How to iterate through the characters using regular expressions? 如何使用正则表达式从乱码的文本中提取一些子文本模式? - How can I extract some patterns of sub text from a gibberish looking text using regular expressions? 使用正则表达式避免在文本的开头和结尾使用字符 - Avoid characters at the beginning and at the end of text using regular expressions 如何使用正则表达式过滤删除一些数据框列,但保留包含某些字符的其他列? - How do I remove some data frame columns using regular expressions filtering but keep others that contain some of the characters? 如何在python中使用正则表达式直接在特殊字符之后和数字之前找到单词? - How to find the word directly after a special character and before a number using regular expressions in python? 如何使用正则表达式获取包含空格和字符的文本? - How to get text with both spaces and characters using a regular expression? 如何使用正则表达式摆脱重复的特殊字符? - How do I get rid of repeating special characters with regular expressions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM