正则表达式最小长度

时间:2019-11-06 03:58:26

标签: regex

我有以下正则表达式:

import pandas as pd
import numpy as np

df = pd.DataFrame({'ID':np.arange(1,21)
                  ,'Place':['A']*10+['B']*10
                  ,'Date':d.to_list() * 2
                  ,'event':[0]*5+[1]+[0]*7+[1]+[0]*6
                  ,'Flag':[0,0,-3,-2,-1,1,2,3,4,0,-3,-2,-1,
                          1,2,3,4,0,0,0]},
                 index = np.arange(1,21))

n=3
s = df['event'].rolling(n*2+1, center=True, min_periods=1).max()
s = s.cumsum()

l = ((s - s.where(s.duplicated()).ffill())).fillna(0) 
l.update(l[l>n]+1)

df['Flag'] = (l - n-1).where(l.gt(0), 0)
print(df)

是否可以断言 ID Place Date event Flag 1 1 A 2019-01-01 0 0.0 2 2 A 2019-01-02 0 0.0 3 3 A 2019-01-03 0 -3.0 4 4 A 2019-01-04 0 -2.0 5 5 A 2019-01-05 0 -1.0 6 6 A 2019-01-06 1 1.0 7 7 A 2019-01-07 0 2.0 8 8 A 2019-01-08 0 3.0 9 9 A 2019-01-09 0 4.0 10 10 A 2019-01-10 0 0.0 11 11 B 2019-01-01 0 -3.0 12 12 B 2019-01-02 0 -2.0 13 13 B 2019-01-03 0 -1.0 14 14 B 2019-01-04 1 1.0 15 15 B 2019-01-05 0 2.0 16 16 B 2019-01-06 0 3.0 17 17 B 2019-01-07 0 4.0 18 18 B 2019-01-08 0 0.0 19 19 B 2019-01-09 0 0.0 20 20 B 2019-01-10 0 0.0 + With ThisWorkbook.Sheets(1) .Range("D5").Value = Counts("RDG1", "PW", "Sewer Works") .Range("E5").Value = Counts("RDG1", "PW", "Sewer Works", "PASS") .Range("F5").Value = Counts("RDG1", "PW", "Sewer Works", "FAIL") 'other counts here End With 'Get row counts from sheet2 ' Optional 4rd parameter for PASS/FAIL Function Counts(v1 As String, v2 As String, v3 As String, Optional v4 As String = "") As Long Dim rng1 As Range, rng2 As Range, rng3 As Range, rng4 As Range With ThisWorkbook.Sheets(2) Set rng1 = .Range("C7:C10000") Set rng2 = .Range("D7:D10000") Set rng3 = .Range("G7:G10000") Set rng4 = .Range("I7:I10000") If Len(v4) > 0 Then Counts = Application.CountIfs(rng1, v1, rng2, v2, rng3, v3, rng4, v4) Else Counts = Application.CountIfs(rng1, v1, rng2, v2, rng3, v3) End If End With End Function 的长度至少为8个字符?换句话说,这应该匹配:

^(?P<wrap_0>\()?[-+]?((?P<whole_part>\d+\.?)|(?P<decimal_part>\d*\.\d+))(?(wrap_0)\))$

此处是现有的正则表达式:https://regex101.com/r/rTrCeB/1(PCRE / php样式)。

2 个答案:

答案 0 :(得分:2)

您可以在此处使用替代:

^(\d{8,}|(?=[0-9.]{8,})\d+(?:\.\d+))$

Demo

以下是正则表达式的解释:

^                     from the start of the string
(
    \d{8,}            match a pure number (no decimal component) of 8 or more digits
    |                 OR
    (?=[0-9.]{8,})    assert that 8 or more digits or decimal point occurs
    \d+(?:\.\d+)      then match a number followed by a decimal component
)
$                     end of the string

答案 1 :(得分:2)

您可以使用正向前瞻来断言至少8个数字或右边的点,然后将1个以上的数字与可选的小数部分匹配。

^(?=[\d.]{8,}$)\d+(?:\.\d+)?$

部分

  • ^字符串的开头
  • (?=[\d.]{8,}$)正向前进,用点或数字断言8次或更多次
  • \d+匹配
  • (?:\.\d+)?可以选择匹配点和1个以上的数字
  • $字符串结尾

Regex demo

相关问题