import 'package:flutter/material.dart';
class Temp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
body: SafeArea(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 32.0, vertical: 32.0),
child: Column(
children: <Widget>[
TextFormField(),
Expanded(
child: Container(
width: 224.0,
color: Colors.yellow,
child: Center(
child: Container(
width: 224.0, height: 224.0,
color: Colors.red,
),
),
),
),
]
),
),
),
);
}
}
我的数据框看起来像上面的一样。我的目标是用与import pandas as pd
dataframe = pd.DataFrame({'Data' : ['The **ALI**1929 for 90 days but not 77731929 ',
'For all **ALI**1952 28A 177945 ',
'But the **ALI**1914 and **ALI**1903 1912',],
'ID': [1,2,3]
})
Data ID
0 The **ALI**1929 for 90 days but not 77731929 1
1 For all **ALI**1952 28A 177945 2
2 But the **ALI**1914 and **ALI**1903 1912 3
关联的OLDER
或以下的任何数字替换1929
一词。因此**ALI**
将是**ALI**1929
,而**ALI**OLDER
也将是ALI**1903
,但**ALI**OLDER
将保持不变。我从https://developer.apple.com/library/archive/documentation/CoreBluetooth/Reference/AppleNotificationCenterServiceSpecification/Specification/Specification.html#//apple_ref/doc/uid/TP40013460-CH1-SW7开始尝试
**ALI**1952
但是对于我想要的来说,这并不是很好。我想要这样的输出
dataframe['older'] = dataframe['Data'].str.replace(r'(?<!\d)(\d{3})(?!\d)', 'OLDER')
如何更改我的正则表达式 Data ID older
0 The ALI**OLDER for 90 days but not 77731929
1 For all ALI**1952 28A 177945
2 But the ALI**OLDER and ALI**OLDER 1912
?
答案 0 :(得分:1)
您可以使用此
(?<=\*)(?:0\d{3}|1[0-8]\d{2}|19[0-2]\d)(?!\d)
(?<=\*)
-前应加*
(?:0\d{3}|1[0-8]\d{2}|19[0-2]\d)
0\d{3}
-匹配0000 to 0999
之间的任何4位数字|
-交替1[0-8]\d{2}
-匹配1000 to 1899
之间的任何4位数字|
-交替19[0-2]\d
-匹配任意4位数字1900 to 1929
(?!\d)
-不应跟数字答案 1 :(得分:0)
将str.extractall
和np.where
与str.replace
一起使用:
nums = dataframe['Data'].str.extractall('(?<=\*\*ALI\*\*)(\d+)').astype(int).unstack()
dataframe['older'] = np.where(nums.le(1929).any(axis=1),
dataframe['Data'].str.replace('(?<=\*\*ALI\*\*)(\d+)', 'OLDER'),
dataframe['Data'])
输出
Data ID older
0 The **ALI**1929 for 90 days but not 77731929 1 The **ALI**OLDER for 90 days but not 77731929
1 For all **ALI**1952 28A 177945 2 For all **ALI**1952 28A 177945
2 But the **ALI**1914 and **ALI**1903 1912 3 But the **ALI**OLDER and **ALI**OLDER 1912
答案 2 :(得分:0)
如我所见,正则表达式应与pyredis.Redis(host='docker.for.mac.localhost', port=6379)
( nnnn -4位数字)匹配,并且:
**ALI**nnnn
-应该(始终)被删除。**
-应该保持不变。ALI**
替换。在这种情况下,不需要复杂的正则表达式。 整个逻辑可以包含在“替换”功能中。
定义如下:
OLDER
然后将def repl(mtch):
g1, g2 = mtch.group(1), mtch.group(2)
return g1 + (g2 if int(g2) > 1929 else 'OLDER')
与该功能配合使用:
str.replace
请注意,我还更改了正则表达式,定义了2个捕获组。
答案 3 :(得分:0)
df.Data = df.Data.str.replace(r'\*\*(ALI\*\*)(\d{4})(?!\d)', repl)
dataframe.Data.str.replace(r"(?<=\*ALI[*]{2})1[0-9](?:(?:[0-4][0-9])|5[0-1])\b","OLDER")
Out[364]:
0 The **ALI**OLDER for 90 days but not 77731929
1 For all **ALI**1952 28A 177945
2 But the **ALI**OLDER and **ALI**OLDER 1912
Name: Data, dtype: object
以`* ALI ** (?<=\*ALI[*]{2})
,即10-19 1[0-9]
从外部“非捕获”组开始
(?:
,即00-49,但未被捕获(?:[0-4][0-9])
,即50-51 |5[01]
未捕获组的结尾)
边界答案 4 :(得分:0)
定义可调用的自定义repl
并将其与str.replace
一起使用
repl = lambda m: m.group(1) if int(m.group(1)) > 1929 else 'OLDER'
df.Data.str.replace(r'(?<=\*\*ALI\*\*)(\d+)', repl)
Out[662]:
0 The **ALI**OLDER for 90 days but not 77731929
1 For all **ALI**1952 28A 177945
2 But the **ALI**OLDER and **ALI**OLDER 1912
Name: Data, dtype: object