繁体   English   中英

如何使用正则表达式更改字符串中的数字

[英]how to change digits in a string using regex

我有一个字符串像..

'1.5"x3"x10" hey 7" x 4"x 2" how 9.5" x 9.5" x 7.5" are 7.1"x 4"x 2" you ..and rest of our conversation

我想要的是将字符串转换成..

'1.5x3x10 hey 7x4x2 how 9.5x9.5x7.5 are 7.1x4x2 you.. and rest of our conversation

简而言之,删除数字之间的空格和"

我试图通过做找到模式。

stuff = re.findall('(\d+\.\d+|\d+)?["]\s?x\s?(\d+\.\d+|\d+)?["]\s?x\s?(\d+\.\d+|\d+)?["]',strings)
print sub

它返回我

[('1.5', '3', '10'), ('7', '4', '2'), ('9.5', '9.5', '7.5'), ('7.1', '4', '2')]

所以我尝试了

stuff = re.findall('\d+["]\s?x\s?\d+["]\s?x\s?\d+["]',strings)
print stuff

它返回我

['5"x3"x10"', '7" x 4"x 2"', '1"x 4"x 2"']

它不包含任何数字..我如何将我的字符串转换为所需的数字? 有什么帮助吗?

如果您真的想一步一步做,就必须对所有情况进行多次前瞻/后顾之忧(这是所有问题都被这个案例捕获的一个问题):

import re

my_str = '\'1.5"x3"x10" hey 7" x 4"x 2" how 9.5" x 9.5" x 7.5" are 7.1"x 4"x 2" you ..and rest of our conversation'

mod_str = re.sub(r'(?<=[\dx])["\s]+(?=[x\s])|(?<=x)\s(?=\d)', '', my_str)
print(mod_str)

让您:

'1.5x3x10 hey 7x4x2 how 9.5x9.5x7.5 are 7.1x4x2 you ..and rest of our conversation

如果将其分为多个步骤,可能会更快(更容易捕获异常值)。

说明:

这里有两种搜索模式, (?<=[\\dx])["\\s]+(?=[x\\s])(?<=x)\\s(?=\\d) ,它们是分开的by |表示一个或另一个(以从左到右的方式,因此如果第一个组捕获了一部分内容,则第二组将不会在其上执行)。

首先:

(?<=            positive non-capturing lookbehind, capture the next segment only if match
  [\dx])        match a single digit (0-9) or the 'x' character
)
  ["\s]+        match one or more " characters or whitespace
(?=             positive non-capturing lookahead, capture the previous segment only if match
  [x\s]         match a single whitespace or 'x' character
)

第二:

(?<=            positive non-capturing lookbehind, capture the next segment only if match
  x             match the 'x' character
)
\s              match a single whitespace
(?=             positive non-capturing lookahead, capture the previous segment only if match
  \d            match a single digit (0-9)
)

前者负责选择数字周围的空格和引号,而后一种则扩展了选择“ x”字符周围的空格的能力,只有在其后跟数字以增加第一个模式的不足之处。 它们一起匹配正确的引号和空格,然后使用re.sub()方法将其替换为空字符串。

zwer显然是regex的高手。 但是,您可能对替代方法感兴趣,该方法有时可以使用更简单的表达式。 它涉及使用re模块来标识要更改的字符串,然后使用Python函数进行操作。

在这种情况下,我们要识别带小数或不带小数的数字,始终后跟""x有时在一个或多个空格之前或之后。此代码使用带有备用表达式的正则表达式查找两者,并将查找到的内容传递给replacer并保留此功能可丢弃不需要的字符。

>>> import re
>>> quest = '1.5"x3"x10" hey 7" x 4"x 2" how 9.5" x 9.5" x 7.5" are 7.1"x 4"x 2" you ..and rest of our conversation'
>>> def replacer(matchobj):
...     for group in matchobj.groups():
...         if group:
...             return group.replace(' ', '').replace('"', '')
... 
>>> re.sub(r'([0-9\.]+\")|(\s*x\s*)', replacer, quest)
'1.5x3x10 hey 7x4x2 how 9.5x9.5x7.5 are 7.1x4x2 you ..and rest of our conversation'

sub的Python文档中的详细信息。

我在这里不会太复杂。

我只一次匹配一组尺寸,然后替换空白和双引号。

(\\d+(?:\\.\\d+)?(?:\\s*"\\s*x\\s*\\d+(?:\\.\\d+)?){2}\\s*")

展开式

 (                             # (1 start)
      \d+ 
      (?: \. \d+ )?
      (?:
           \s* " \s* x \s* 
           \d+ 
           (?: \. \d+ )?
      ){2}
      \s* "
 )                             # (1 end)

Python演示http://rextester.com/HUIYP80133

Python代码

import re

def repl(m):
    contents = m.group(1)
    return re.sub( r'[\s"]+','', contents )

str = '\'1.5"x3"x10" hey 7" x 4"x 2" how 9.5" x 9.5" x 7.5" are 7.1"x 4"x 2" you ..and rest of our conversation'

newstr = re.sub(r'(\d+(?:\.\d+)?(?:\s*"\s*x\s*\d+(?:\.\d+)?){2}\s*")', repl, str)

print newstr

输出量

'1.5x3x10 hey 7x4x2 how 9.5x9.5x7.5 are 7.1x4x2 you ..and rest of our conversation

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM