如何仅从字符串中提取数字,除了数字开头带有特殊字符的子字符串

How to extract only numbers from string except substring with special character in beginning of numbers

我的计划是从字符串中提取除具有特殊字符的数字以外的数字。我的意思是? 请想象以下(如 Excel 公式):

=$A12+A5+A6789

我需要提取开头不存在任何字符 $ 的数字,因此正确的正则表达式的结果应该是:

12
6789

我在使用以下正则表达式的地方做了一些调查:

/[A-Z][0-9]+(?![/W])/g

其中提取:

A12
A6789

我正在考虑使用嵌套正则表达式(另外从该结果中提取数字)但我不知道是否可行。到目前为止,我在 javascript 中的源代码:

http://jsfiddle.net/janzitniak/fvczu7a0/7/

此致

一月

const regex = /(?<ref>$?[A-Z]+(?<!$)[0-9]+)/g;
const str = `=$A12+A5+A6789`;

const refs = [...(str.matchAll(regex) || [])].map(result => result.groups.ref);

console.log(refs)

匹配任何包含 A-Z 一次或多次且前面有 $ 零次或一次,后跟 0-9 一次或多次但前面没有 $,所有后面都跟着 + 零次或一次的字符串。

您忽略所有匹配的组,但捕获您想要的组,引用为 ref(您可以随意命名)。

输出:

["$A12","A6789"]

如果你只想要数字部分,你可以使用:

const regex = /$?[A-Z]+(?<!$)(?<num>[0-9]+)/g;
const str = `=$A12+A5+A6789`;
const nums = [...(str.matchAll(regex) || [])].map(result => +result.groups.num);
console.log(nums)

输出:

[12, 6789]

const charSequence = '=$A12+A5+A6789';

const numberList = (charSequence
  .split(/$\d+/)       // - split at "'$' followed by one or more numbers".
  .join('')             // - join array of split results into string again.
  .match(/\d+/g) || []) // - match any number-sequence or fall back to empty array.
  .map(str => +str);    // - typecast string into number.
//.map(str => parseInt(str, 10)); // parse string into integer.

console.log('numberList : ', numberList);
.as-console-wrapper { min-height: 100%!important; top: 0; }

@ibraheem can you help me once again please? How can I increment ref output if I want to have the following result ["$A13","A6790"]? - JanZitniak 23 mins ago

... split/join/match 方法可以非常快速地迭代,因此证明它非常灵活。

const charSequence = '=$A13+A5+A6790';

const numberList = (charSequence
  .split(/$\d+/)       // - split at "'$' followed by one or more numbers".
  .join('')             // - join array of split results into string again.
  .match(/$*[A-Z]\d+/g) || []);  // - match any sequence of an optional '$' followed
                                  //   by 1 basic latin uppercase character followed
                                  //   by one or more number character(s).

console.log('numberList : ', numberList);
.as-console-wrapper { min-height: 100%!important; top: 0; }

Peter thank you for your quick response about increment but on start I have const charSequence = '=$A12+A5+A6789'; and as output I need ["$A13","A6790"]. – JanZitniak

...好吧,终于可以全面了解整个问题了...这是 (1) 摆脱不必要的模式 ...(2) 在特定模式中匹配数字 并以某种方式记住后者 (3) 增加这些数字并以某种方式将它们重新加工成它们的remembered/recallable模式.

const anchorSequence = '=$A12+A5+A6789';

const listOfIncrementedAnchorCoordinates = [...(anchorSequence

  // - split at "'$' followed by one or more numbers".
  .split(/$\d+/)

  // - join array of split results into string again.
  .join('')

  // - match any sequence of an optional '$' followed by 1 basic latin 
  //   uppercase character followed by one or more number character(s)
  //   and store each capture into a named group.
  .matchAll(/(?<anchor>$*[A-Z])(?<integer>\d+)/g) || [])

  // map each regexp result from a list of RegExpStringIterator entries.
].map(({ groups }) => `${ groups.anchor }${ (+groups.integer + 1) }`);

console.log('listOfIncrementedAnchorCoordinates : ', listOfIncrementedAnchorCoordinates);
.as-console-wrapper { min-height: 100%!important; top: 0; }

Peter if you are interest(...ed) in another problem I have one. How can I change const anchorSequence = '=$A12+A5+A6789'; to following output ["B5","B6789"]? I mean to change letter to next one in alphabetical order (if it is A then change to B, if it is B change to C and so on) if letter doesn't start with $. In my example it should change only A5 and A6789. – JanZitniak

...稍微思考一下,iterate/refactor 之前的版本到最后一个版本并不难...

const anchorSequence = '=$A12+A5+A6789';

const listOfIncrementedColumns = [...(anchorSequence

  // - split at "'$' followed by 1 basic latin uppercase character 
  //   followed by one or more number character(s)".
  .split(/$[A-Z]\d+/)

  // - join array of split results into string again.
  .join('')

  // - match any sequence of 1 basic latin uppercase character
  //   followed by an optional '$' followed by one or more number
  //   character(s) and store each capture into a named group.
  .matchAll(/(?<column>[A-Z])(?<anchor>$*)(?<row>\d+)/g) || [])

  // map each regexp result from a list of RegExpStringIterator entries.
].map(({ groups }) => [

  // - be aware that "Z" (charCode:90) will be followed by "[" (charCode:91)
  // - thus, the handling of this edge case still needs to be implemented.
  String.fromCharCode(groups.column.charCodeAt(0) + 1),
  groups.anchor,
  groups.row

].join(''));

console.log('listOfIncrementedColumns : ', listOfIncrementedColumns);
.as-console-wrapper { min-height: 100%!important; top: 0; }