带有空格或破折号的任意数字的正则表达式

时间:2018-09-30 23:42:12

标签: javascript regex

我具有根据输入生成正则表达式的功能。我的输入内容不尽相同,可以是破折号,空格和字母数字。

这是真实的结果:

Formula  | Input   | Result
----------------------------
12 A     | 39 A    | True 
888 B    | 42 C    | False
A-12     | O-399   | False
PA-333   | Q-32    | False
ZA-33    | QA-32   | True
A123     | K32     | False
32P      | 8A      | False
3A       | 8A      | True
99C      | C99     | False

如何实现上述真实结果?我的一些代码可以正常工作,但一些测试用例却失败了。

以下是我的代码:

function generateUnitNumber(a){
  if(a == null) return null;
  function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
  }

  function isLetter(s) {
    return s.match("^[a-zA-Z\(\)]+$");
  }

  var z = a.split("-");
  var alpha = "[a-zA-Z]";
  var num = "[^\\d]";
  var partial = [];

  z.forEach(function(a, ind){
    var temp = "";
    var isNum = false;
    for(var i = 0; i < a.length; i++){
    	if(/\s/.test(a[i])){
    	  temp = temp + "[\\S]";
        isNum = false;
    	} 
      
      if(isNumber(a[i])){
        if(isNum === false) {
        	temp = temp + num;
          isNum = true;
        }
        else isNum = true;
        // temp = temp + "{1}";
      }
      if(isLetter(a[i])){
        temp = temp + alpha;
        isNum = false
        // temp = temp + "{1}";
      }
    }
		
    partial.push(temp);
  });
  partial.push("?$");
	console.log("partial: ",partial);
  return partial.join("");
}

 var unit_formula = generateUnitNumber("444 C");
 console.log('unit formula: ', unit_formula);

 var re = new RegExp(unit_formula);
 console.log('Final Result: ' + re.test('32 C'));

2 个答案:

答案 0 :(得分:0)

我认为您可以replace用其等效的正则表达式位来"[a-zA-Z]+"。将有4个组/类别:字母,数字,空格和破折号。这些组中的每一个都将分别替换为:"\\d+"function generateUnitNumber(a) { if(a == null) return null; return a.replace(/[a-zA-Z]+|\d+/g, function(match) { // replace a sequence of letters or a sequence of digits by... if(/[a-zA-Z]+/.test(match)) { // if it is a sequence of letters return "[a-zA-Z]+"; // then by "[a-zA-Z]+" } else if(/\d+/.test(match)) { // if it is a sequence of digits (testing against a regex here is totally unecessary as there will be either a sequence of letters or a sequence of digits, so a simple 'else' will do, but if you do add other groups/categories (see note 1 bellow), you'll need it) return "\\d+"; // then by "\\d+" } }); }

9 C

注释:

  1. 由于空格和破折号将被它们自己替换,所以实际上根本不需要替换它们。除非您希望空格或破折号等于任意数量的空格或破折号(即7 B可以匹配+),否则在这种情况下,您还必须匹配它们并添加一个在它们之后"\\s+"(空格变为"-+",破折号变为replace)。
  2. 我们必须将所有替换分组到一个replace(...).replace(...)...调用中,因为像"\\d+"这样将替换链接起来分别处理每个组可能会很棘手,因为一旦替换了一个组,我们就可以替换再次得出该组的结果(例如,用"d"替换数字,然后用"[a-zA-Z]+"替换"\\[a-zA-Z]++"以获得^,这毫无意义)。
  3. 您应将正则表达式包装在$function generateUnitNumber(a) { if(a == null) return null; return a.replace(/[a-zA-Z]+|\d+/g, function(match) { if(/[a-zA-Z]+/.test(match)) { return "[a-zA-Z]+"; } else if(/\d+/.test(match)) { return "\\d+"; } }); } var testcases = [["12 A","39 A","True"],["888 B","42 C","True"],["A-12","O-399","True"],["PA-333","Q-32","True"],["A123","K32","True"],["32P","8A","True"],["99C","C99","False"]]; testcases.forEach(function(testcase) { var regexText = generateUnitNumber(testcase[0]); var regex = new RegExp("^" + regexText + "$"); console.log( "Formula: " + testcase[0] + "\n" + "Input: " + testcase[1] + "\n" + "Regex: " + regexText + "\n" + "Expected Result: " + testcase[2] + "\n" + "Final Result: " + regex.test(testcase[1]) + "\n" ); });中以使其严格。

示例:

+


编辑:

如果数字和字母字符的数量很重要,则只需将生成的正则表达式中的"123"替换为字母数即可,例如,"\d{3}"将是"\d+" match.length中的。您可以为此使用function generateUnitNumber(a) { if(a == null) return null; return a.replace(/[a-zA-Z]+|\d+/g, function(match) { if(/[a-zA-Z]+/.test(match)) { return "[a-zA-Z]{" + match.length + "}"; } else if(/\d+/.test(match)) { return "\\d{" + match.length + "}"; } }); }

function generateUnitNumber(a) {
    if(a == null) return null;
    return a.replace(/[a-zA-Z]+|\d+/g, function(match) {
        if(/[a-zA-Z]+/.test(match)) {
            return "[a-zA-Z]{" + match.length + "}";
        } else if(/\d+/.test(match)) {
            return "\\d{" + match.length + "}";
        }
    });
}

var testcases = [["12 A","39 A","True"],["888 B","42 C","False"],["A-12","O-399","False"],["PA-333","Q-32","False"],["ZA-33","QA-32","True"],["A123","K32","False"],["32P","8A","False"],["3A","8A","True"],["99C","C99","False"]];

testcases.forEach(function(testcase) {
    var regexText = generateUnitNumber(testcase[0]);
    var regex = new RegExp("^" + regexText + "$");

    console.log(
        "Formula:         " + testcase[0] + "\n" +
        "Input:           " + testcase[1] + "\n" +
        "Regex:           " + regexText + "\n" +
        "Expected Result: " + testcase[2] + "\n" +
        "Final Result:    " + regex.test(testcase[1]) + "\n" 
    );
});

示例:

from flask import Flask
from flask_db2 import DB2

app = Flask(__name__)

app.config['DB2_DATABASE'] = 'sample'
app.config['DB2_HOSTNAME'] = 'localhost'
app.config['DB2_PORT'] = 50000
app.config['DB2_PROTOCOL'] = 'TCPIP'
app.config['DB2_USER'] = 'db2inst1'
app.config['DB2_PASSWORD'] = 'db2inst1'

db = DB2(app) 

cur = db.connection.cursor()
print cur

答案 1 :(得分:0)

如果我了解,以下正则表达式可能会起作用,您想要什么。它要求formulainput以'|'(space|space)分隔。然后,只有当两者均以字母开头,后跟数字或两者都以数字开头,后跟字母时,它才会匹配。它在数字和字母之间留有空格或连字符。

这里是:

(?:\d+[ -]?\w+( \| )?){2}|(?:\w+[ -]?\d+( \| )?){2}