简体   繁体   中英

Regex to replace decimal and integer strings with numbers

In sublime text I have some data that contains numeric strings. Some are decimals and others are integers. I would like to remove the quotes to have them just as numbers. Is there a regular expression I can use to achieve this. I would also need a replace expression to go with it.

Eg

'0'    => 0
'1'    => 1
'1234' => 1234
'1.23' => 1.23

I've extensively searched for a solution with no luck.

Something like this should work: '(\\d+(\\.\\d+)?)' . This will capture the number and place it in a group. You can then replace that with either $1 or \\1 . This should backreference the matched group, which is the number itself.

Regex explanation available here .

Expanding on @npinti's answer;

If you need to work with negative integers as well; in SublimeText

Ctrl+H Find & Replace

Find : '((\\-)?\\d+(\\.\\d+)?)'

Alt+R Find (search) using regular expressions

Replace : \\1

Illustration...

Illustartion

Regex tested with Regex101

整数 - 正则表达式

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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