4Manuals

  • PDF Cloud HOME

在方括号中使用替代项时,Python正则表达式子项无法正常工作 Download

    将我的代码从多处理更改为普通 字符串数组在for循环中初始化时忽略索引0 如何从作为请求传递的输入类型数据绑定中提取数据 预测使用Gunicorn时无法解开物体 如何从列表内的字典中获取键值对的值,并且键以非常特定的字母开头 正则表达式提取复杂文本中的电话号码 'python -m unittest discover'在本地工作,但无法在CircleCi上发现测试 如何在C中删除字符串的最后一部分 输出控制台中的不同格式与Python中的已保存文件 每分钟用动态文件名创建一个新的日志文件

我正在尝试使用Python public static IEnumerable<object> CreateClass<T>(this T[,] array) { int rowCount = array.GetLength(0); int columnCount = array.GetLength(1); var fields = array.SliceHeaderRow().ToList(); //convert 1st row to 1d array var properties = fields .Select(f => new DynamicProperty(f, f.GetType())) .ToList(); Type dynoType = DynamicClassFactory.CreateType(properties); object obj = Activator.CreateInstance(dynoType); int jj; for (int ii = 1; ii < rowCount; ii++) //skip first row { jj = 0; foreach (var field in fields) dynoType.GetProperty(field).SetValue(obj, array[ii, jj], null); jj++; yield return obj; } } public static string[] SliceHeaderRow<T>(this T[,] array) { string[] fieldNames = new string[array.GetLength(0) - 1]; for (int jj = 0; jj < array.GetLength(1); jj++) fieldNames[jj] = array[0, jj].ToString(); return fieldNames; } 模块将字符串中的日期替换为<Month Year>。

我尝试过:

re

但是它返回:

import re
s = "Wikipedia articles containing buzzwords from April 2014\t23"
s = re.sub(r"[January|April|March]\s+\d{1,4}", "<Month Year>", s)

而不是我所期望的:

'Wikipedia articles containing buzzwords from Apri<Month Year>\t23'

我要去哪里错了?

1 个答案:

答案 0 :(得分:1)

括号表示您需要括号内的其他成员(字符)。试试这个:

s = re.sub(r"(January|April|March)\s+\d{1,4}", "<Month Year>", s)

此处的简单示例:

>>> import re
>>> s = "Wikipedia articles containing buzzwords from April 2014\t23"
>>> s = re.sub(r"(January|April|March)\s+\d{1,4}", "<Month Year>", s)
>>> s
'Wikipedia articles containing buzzwords from <Month Year>\t23'



Similar searches
    Githook在GitKraken中预提交错误退出代码1 Google是否使用每个主要社交媒体网站提供的Developer API的许可实例来搜索该网站或进行外部爬网? Android Kotlin:使ScrollBar达到顶部 如何在Akka.Event.LogMessage中修复“输入字符串的格式不正确” 更改Jupyter实验室徽标