English 中文(简体)
如何与“经常表达”或“定期表达”相匹配
原标题:How to match the or " with or " in regular expression
  • 时间:2012-01-13 21:59:18
  •  标签:
  • python
  • regex

The following regular expression is used to extract the URL connection from a page:

LINK_REGEX = re.compile("<a [^>]*href=[ "]([^ "]+)[ "][^>]*>")

<>问题1>> 如何代表以下描述? I mismatch and “ in purpose

<a href="http://www.yahoo.com >

我尝试了以下发言,我没有做任何工作。

>>> page =  <a href="http://www.yahoo.com > 
>>> page
 <a href="http://www.yahoo.com > 
>>> page =  <a href="http://www.yahoo.com  > 
>>> page
 <a href="http://www.yahoo.com> 

< Question2>根据我的理解,经设计,LNK_REGEX将与上述链接相对应,尽管这并不可取。 因此,我如何能够修改定期表述,以强制执行与“......”的匹配。

问题回答

对于问题1,你的第一个做法是行之有效的。

>>> page =  <a href="http://www.yahoo.com > 
>>> len(page)
31
>>> page
 <a href="http://www.yahoo.com > 
>>> page[-1]
 > 
>>> page[-2]
" "
>>> page[-3]
 m 

(I d post this as a comment if I had the privilege.)

如果你重新尝试使用超文本,则建议你不要使用reg。 如果你使用像“美丽”或“乐施”这样的“超声望”模块,那么你就能够挽救许多 has和问题。

第二,几乎每当你重新使用规章时,就必须确保在座标上<条码>/条码>。

LINK_REGEX = re.compile(r”<a [^>]*href=[“][[^”+] [[][>]*>”

这将确保物品能够适当逃脱。

如果你确实需要使用reg,那么“9000”答案就会为你工作。

([ "]).+1 will match a quoted string with matcing quotes. The expression in parens (match group) will match a single or double quote, and 1 will match whatever the first match group have matched (this is called backreference ).

Note that the quotes are not escaped in any way in the expressions to make them more readable. Your regex strings may need to escape at least one kind of quotes.

采用两种规章:

<as*[^>]*href="([^"]+)"[^>]*>  # double quoted strings
<as*[^>]*href= ([^ ]+) [^>]*>  # single quoted strings

<代码>href的内容将放在第二组。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签