删除空格和句号 [英] Remove empty spaces and period

查看:41
本文介绍了删除空格和句号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用此正则表达式:

I cannot get this regex to work:

"4.  182 ex" (number, period, 2 blank spaces, 3 numbers, blank space, 2 characters"

正则表达式语法应返回4182"并删除句点、空格和字符.

The regex syntax should return "4182" and remove period, blank spaces, and characters.

你能帮我吗?

编辑!!!

谢谢大家,但我错过了关键问题:

Thanks everyone but I missed the key question:

a) 当同一行包含特定文本(例如magic")时,正则表达式应找到值 (4182),例如:

a) the regex shall only find the value (4182) when the same line contains a specific text for example "magic", so for example:

万智牌 4. 182 前"

"Magic 4. 182 ex"

b) 当表包含特定文本(例如Magic")时,正则表达式应仅"找到值 (4182):

b) the regex shall "only" find the value (4182) when the table contains a specific text for example "Magic":

《万智牌 4. 182 前》

"Magic 4. 182 ex

Lisefeo 2. 123 英尺

Lisefeo 2. 123 fg

Nioos 3. 124 df"

Nioos 3. 124 df"

特定文本 = 完全匹配或包含这些字符

specific text = exact match or contains those charachters

我到目前为止尝试过的正则表达式,但它是否适用于整个表格(不仅仅是一行)?

My regex that I've tried so far but does it work for a whole table (not just a line) ?

(魔法.*?(\d).\s\s(\d{3})\s\w\w)

(Magic.*?(\d).\s\s(\d{3})\s\w\w)

推荐答案

只需删除所有不是数字的字符:

Just remove all characters that are not digit:

Perl:

$string =~  s/\D+//g;

或php:

$string = preg_replace('/\D+/', '', $string);

根据您更新的问题,您可以:

According to your updated question, you could do:

$string =~ s/^Magic(\d+)\.  (\d{3})\b.*$/$1$2/

或者,使用 php:

$string = preg_replace('/^Magic(\d+)\.  (\d{3})\b.*$/', '$1$2', $string);

这篇关于删除空格和句号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆