簡體   English   中英

Python正則表達式是否支持像Perl的\\ G?

[英]Do Python regexes support something like Perl's \G?

我有一個Perl正則表達式( 在這里顯示,雖然理解整個事情不是必須回答這個問題)包含\\ G元字符。 我想將它翻譯成Python,但Python似乎不支持\\ G. 我能做什么?

試試這些:

import re
re.sub()
re.findall()
re.finditer()

例如:

# Finds all words of length 3 or 4
s = "the quick brown fox jumped over the lazy dogs."
print re.findall(r'\b\w{3,4}\b', s)

# prints ['the','fox','over','the','lazy','dogs']

我知道我遲到了,但這里是\\G方法的替代品:

import re

def replace(match):
    if match.group(0)[0] == '/': return match.group(0)
    else: return '<' + match.group(0) + '>'

source = '''http://a.com http://b.com
//http://etc.'''

pattern = re.compile(r'(?m)^//.*$|http://\S+')
result = re.sub(pattern, replace, source)
print(result)

輸出(通過Ideone ):

<http://a.com> <http://b.com>
//http://etc.

我們的想法是使用匹配兩種字符串的正則表達式:URL或注釋行。 然后使用回調(委托,閉包,嵌入代碼等)來找出匹配的那個並返回相應的替換字符串。

事實上,這是我的首選方法,即使是支持\\G口味。 即使在Java中,我也必須編寫一堆樣板代碼來實現回調。

(我不是一個Python人,所以請原諒我,如果代碼是非常pythonic。)

您可以使用re.match匹配錨定模式。 re.match只會在文本的開頭(位置0)或您指定的位置匹配。

def match_sequence(pattern,text,pos=0):
  pat = re.compile(pattern)
  match = pat.match(text,pos)
  while match:
    yield match
    if match.end() == pos:
      break # infinite loop otherwise
    pos = match.end()
    match = pat.match(text,pos)

這只會匹配給定位置的模式,以及之后跟隨0個字符的任何匹配。

>>> for match in match_sequence(r'[^\W\d]+|\d+',"he11o world!"):
...   print match.group()
...
he
11
o

Python的regexen沒有/ g修飾符,因此沒有\\ G regex令牌。 可惜,真的。

不要試圖將所有內容都放在一個表達式中,因為它變得非常難以閱讀,翻譯(如您自己所見)和維護。

import re
lines = [re.sub(r'http://[^\s]+', r'<\g<0>>', line) for line in text_block.splitlines() if not line.startedwith('//')]
print '\n'.join(lines)

從字面上翻譯Perl時,Python通常不是最好的,它有自己的編程模式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM