如何在还包含字母的字符串中增加一个数字? [英] How to increase a number in a string that also contains letters?
本文介绍了如何在还包含字母的字符串中增加一个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想对字符串中的数字执行算术运算.
I want to perform arithmetic operations on numbers in a string.
例如:SGJKR67
应该变成 SGJKR68
.
另一个:NYSC34
应该变成 NYSC35
.
Another one: NYSC34
should become NYSC35
.
只有数字发生了变化,在本例中,两者都增加了 1.
Only numbers are changed, in this example both are increased by one.
推荐答案
使用正则表达式和捕获组可以解决您的问题:
Using regex and Capturing Groups can solve your problem:
$reg = [regex]::new("([A-Z]+)(\d+)")
$m = $reg.Match("SGJKR67")
$digits = $m.Groups[2] # will be 67
$digits = $digit + 1; # or apply anything you want
$result = "$($m.Groups[1])$digits" # will be SGJKR and 68.
您的比赛将有 3 个小组:
You will have 3 groups for your matches:
- 整个词".
- 字母
- 数字.
这篇关于如何在还包含字母的字符串中增加一个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文