正则表达式仅匹配{n}个数字,且不被任何东西包围

时间:2019-03-23 22:42:10

标签: c# regex

我试图或多或少地仅匹配{n}个数字,并且可能会被字符或特殊符号包围

示例:
假设{n} = {14}

* 12345678901234 * 300 确定

12345678901234x21 确定

* 123456789012345 * 300 不好

12345678901234 确定

123456789012345 不好

2 个答案:

答案 0 :(得分:1)

(?:^|\D)(\d{14})(?:\D|$)

这里是Live Demo

答案 1 :(得分:1)

您可以使用负数lookarounds来断言直接在左边和右边不是数字并匹配14位数字:

import random

currentpath = [(0,0)]
length = 2 #any actual grid is 3x3 length is 2 however
height = 2
initial = (0,0)
allpaths = []

def backtrack(currentpath,currentnode):
    if(currentnode == (0,0) and len(currentpath)>1):
        return True
    directions = [0,1,2,3]
    while(len(directions) > 0):
        x = random.choice(directions)
        if(x == 0):
            #left
            nextnode = (currentnode[0] + 1, currentnode[1])
            if(currentnode[0] == length or (nextnode in currentpath and nextnode != (0,0)) or (nextnode ==(0,0) and len(currentpath)<4)):
                directions.remove(x)
                continue
            else :
                currentpath.append(nextnode)
                if(backtrack(currentpath,nextnode)):
                    return True
                else :
                    directions.remove(x)
                    currentpath.remove(nextnode)
                    continue
        if(x == 1):
            #right
            nextnode = (currentnode[0] - 1, currentnode[1])
            if (currentnode[0] == 0 or (nextnode in currentpath and nextnode != (0,0)) or (nextnode ==(0,0) and len(currentpath)<4)):
                directions.remove(x)
                continue
            else:
                currentpath.append(nextnode)
                if(backtrack(currentpath,nextnode)):
                    return True
                else :
                    directions.remove(x)
                    currentpath.remove(nextnode)
                    continue
        if(x == 2):
            #up
            nextnode = (currentnode[0], currentnode[1] + 1)
            if (currentnode[1] == height or (nextnode in currentpath and nextnode != (0,0)) or (nextnode ==(0,0) and len(currentpath)<4)):
                directions.remove(x)
                continue
            else:
                currentpath.append(nextnode)
                if(backtrack(currentpath,nextnode)):
                    return True
                else :
                    directions.remove(x)
                    currentpath.remove(nextnode)
                    continue
        if(x == 3):
            #down
            nextnode = (currentnode[0], currentnode[1] - 1)
            if (currentnode[1] == 0 or (nextnode in currentpath and nextnode != (0,0)) or (nextnode ==(0,0) and len(currentpath)<4)):
                directions.remove(x)
                continue
            else:
                currentpath.append(nextnode)
                if(backtrack(currentpath,nextnode)):
                    return True
                else :
                    directions.remove(x)
                    currentpath.remove(nextnode)
                    continue
    if(len(directions)==0):
        return False

backtrack(currentpath,initial)
print (currentpath)

.NET regex demo

相关问题