3

我想将已知数字(浮点数或整数)与字符串匹配并找到该重复的数字。

像“22.5 98 12 22 322”中的第 22 场比赛

我怎么能用正则表达式做到这一点?

4

6 回答 6

4

尝试使用这个:

if (numbers.indexOf("22") != -1) {
   //The number 22 is in the String
}

它不完全是一个正则表达式,但为什么在你不需要它的时候使用它呢?

于 2012-04-27T14:33:50.720 回答
2
str.match(/substr/g).length

返回“str”中“substr”的出现次数。例如:

str = "22.5 98 12 22 322"
num = str.match(/22/g).length
alert(num) // 3

如果要匹配整数而不仅仅是子字符串,请确保数字前后都有一个空格:

str = "22.5 98 12 22 322"
num = str.match(/(^|\s)22($|\s)/g).length
alert(num) // 1

另一种选择,没有正则表达式:

str = "22.5 98 12 22 322"
num = str.split(" ").filter(function(item) {
    return Number(item) === 22;
}).length;
于 2012-04-27T15:13:49.513 回答
1

如果你的意思是你应该只找到在同一个字符串中重复的数字,你可以使用这样的东西:

var test_str = '22 34 55 45345 34 22 555';
var matches  = test_str.match(/(\b[0-9]+\b)(?=.+\b\1\b)/g); 
// matches contains ["22", "34"], but not "55"
于 2012-04-27T14:42:16.990 回答
0
var str = "22.5 98 12 22 322";
var pattern = /22/g;
if (pattern.test(str)) {
    // it matches "22", do your logic here...
}

有关更多信息,请参见RegExpRegExp.test(string)

于 2012-04-27T14:37:31.630 回答
0

这是您探索正则表达式的好工具 :) http://www.regular-expressions.info/javascriptexample.html

从上面的网站采用:

function demoShowMatchClick() {
  var re = new RegExp("(?:^| )+(22(?:$| ))+");
  var m = re.exec("22.5 98 12 22 322");
  if (m == null) {
    alert("No match");
  } else {
    var s = "Match at position " + m.index + ":\n";
   for (i = 0; i < m.length; i++) {
     s = s + m[i] + "\n";
   }
   alert(s);
 }
}

这将为您提供所有匹配项,并要求以字符串的开头或结尾或空格分隔数字。

于 2012-04-27T14:37:38.883 回答
0

将整数/浮点数从文本关联到正则表达式
非常棘手,因为正则表达式没有这样的概念。

使用动态输入,所有工作都是以编程方式构建正则表达式。
这个例子不包括科学记数法,但它可以。

伪代码:

If the input matches this parse regex  
    /^  (?=.*\d)     // must be a digit in the input
        ([+-]?)      // capt (1), optional +/-
        [0]*         // suck up all leading 0's
        (\d*)        // capt (2), optional digits, will start with [1-9]
        ([.]?)       // capt (3), optional period
        (\d*?)       // capt (4), non-greedy optional digits, will start with [1-9]
        [0]*         // suck up all trailing 0's
     $/x

    sign = '[+]?';

    If !length $2 AND !length $4
         sign = '[+-]';
    Else
        If $1 == '-'
            sign = '[-]';
        Endif
    Endif

    whole = '[0]*';

    If  length $2
         whole = '[0]*' + $2;
    Endif

    decfrac = '[.]? [0]*';

    If  length $4
         $decfrac = '[.] ' + $4 + '[0]*';
    Endif

    regex = '/(?:^|\s)(?=\S*\d) ' +  "$sign $whole $decfrac" + ' (?:\s|$)/x';

Else
    //  input is not valid
Endif

这是一个 Perl 测试用例。
生成的动态正则表达式尚未经过测试,
我认为它可以工作,但我可能是错的。

@samps = (
'00000.0',
 '+.0',
 '-.0',
 '0.',
 '-0.00100',
 '1',
 '+.1',
 '+43.0910',
 '22',
 '33.e19',
);


for (@samps) 
{
   print "\nInput:  $_\n";

   if( /^ (?=.*\d) ([+-]?) [0]*(\d*) ([.]?) (\d*?)[0]*$/x )   # 1,2,3,4
   {

      my ($sign, $whole, $decfrac);

      $sign = '[+]?';

      if ( !length $2  && !length $4) {
           $sign = '[+-]';
      } elsif ($1 eq '-') {
           $sign = '[-]';
      }

      $whole = '[0]*';

      if ( length $2) {
          $whole = '[0]*'.$2;
      }

      $decfrac = '[.]? [0]*';

      if ( length $4) {
          $decfrac = '[.] '.$4.'[0]*';
      }


      my $regex = '/(?:^|\s)(?=\S*\d) '. "$sign $whole $decfrac" . ' (?:\s|$)/x';

      print "Regex:  $regex\n";
   }
   else {
      print "**input  '$_'  is not valid\n";
      next;
   }
}

输出

Input:  00000.0
Regex:  /(?:^|\s)(?=\S*\d) [+-] [0]* [.]? [0]* (?:\s|$)/x

Input:  +.0
Regex:  /(?:^|\s)(?=\S*\d) [+-] [0]* [.]? [0]* (?:\s|$)/x

Input:  -.0
Regex:  /(?:^|\s)(?=\S*\d) [+-] [0]* [.]? [0]* (?:\s|$)/x

Input:  0.
Regex:  /(?:^|\s)(?=\S*\d) [+-] [0]* [.]? [0]* (?:\s|$)/x

Input:  -0.00100
Regex:  /(?:^|\s)(?=\S*\d) [-] [0]* [.] 001[0]* (?:\s|$)/x

Input:  1
Regex:  /(?:^|\s)(?=\S*\d) [+]? [0]*1 [.]? [0]* (?:\s|$)/x

Input:  +.1
Regex:  /(?:^|\s)(?=\S*\d) [+]? [0]* [.] 1[0]* (?:\s|$)/x

Input:  +43.0910
Regex:  /(?:^|\s)(?=\S*\d) [+]? [0]*43 [.] 091[0]* (?:\s|$)/x

Input:  22
Regex:  /(?:^|\s)(?=\S*\d) [+]? [0]*22 [.]? [0]* (?:\s|$)/x

Input:  33.e19
**input  '33.e19'  is not valid
于 2012-04-27T18:23:29.023 回答