python正则表达式组编号

时间:2017-03-26 09:53:37

标签: python regex

我试图从这种字符串中截取数字:

df %>% 
  mutate(new = as.integer(name %in% c('A', 'C'))) %>% 
  group_by(type, new) %>% 
  summarise(name = paste0(name, collapse = ''), val = sum(val)) %>%
  ungroup() %>% 
  select(-new) %>% 
  filter(nchar(name) > 1) %>% 
  bind_rows( df) %>% 
  arrange(val)

# A tibble: 8 × 3
#    type  name   val
#  <fctr> <chr> <dbl>
#1     10     A     1
#2     10     B     2
#3     10     C     3
#4     10    AC     4
#5     20     A     4
#6     20     B     5
#7     20     C     6
#8     20    AC    10

首先删除所有空格:

"30098.904999  5  ABC                    Da   d 8 06 01 20 00 80 11 C0 04"

然后我试图应用模式:

test = ' '.join(test.split())
但是,仍然没有结果:

pattern  = r"(\d+.\d+) (\d+) ABC Da d 8 (\d\d) (\d\d) (\d\d) (\d\d) (\d\d) (\d\d) (\d\d) (\d\d)"

如果我将第一个更改为50.309951,那么它可以正常工作。 第一个数字是时间戳,其中的数字量可以变化..

任何帮助都非常欢迎! :) thx提前 学家

3 个答案:

答案 0 :(得分:0)

为什么不在删除空格字符后拆分字符串?

test = ' '.join(test.split())
像这样?

您将收到一系列项目

['30098.904999', '5', 'ABC', 'Da', 'd', '8', '06', '01', '20', '00', '80', '11', 'C0', '04']

答案 1 :(得分:0)

由于C0\d\d不匹配而导致的错误。您可以对该部分使用\d\w。但作为一种更通用的方法,您可以使用re.findall()来捕获所有数字:

In [24]: test = "30098.904999  5  ABC                    Da   d 8 06 01 20 00 80 11 C0 04"

In [27]: re.findall(r'\d+(?:\.\d+)?', test)
Out[27]: ['30098.904999', '5', '8', '06', '01', '20', '00', '80', '11', '0', '04']
# If you want C0 too:
In [28]: re.findall(r'\w?\d+(?:\.\d+)?', test)
Out[28]: ['30098.904999', '5', '8', '06', '01', '20', '00', '80', '11', 'C0', '04']

答案 2 :(得分:0)

  1. 您不需要使用 $members = Member::with(['ledger' => function($q) { $q->where('created_at', '2017-03-26 14:15:26'); }])->where('type','=','5')->paginate(10); ,因为您可以使用split来匹配一个或多个空格
  2. 您的正则表达式也需要更正。
  3. 您可以使用:

    \s+

    RegEx Demo

相关问题