正则表达式验证字符串是否包含特殊字符数的特殊计数

时间:2017-05-01 09:53:17

标签: javascript regex

我正在开发一种模式,如果它不包含两个匹配的#,则验证字符串。这是代码:

^[^\!|\@|\$|\%|\^|\&|\*|\+][\w]*([\w ]+\#[\w ]*){0,2}$
  

[!^ | \ @ | \ $ | \ | \ ^ | \&安培; | * | +]

这是一组不可接受的符号。

此外,如果字符串包含其他符号( - _,。/),则该模式应验证字符串。每个符号都应该有自己的计数器,不应该在任何位置匹配两次以上。

例如,如果我有这样的字符串:

  

Mcloud博士。 #33 /#45,一些文字,一些文字

它应该是有效的。但在这种情况下不应该:

  

Mcloud博士。 ### 33 / $#45,####,----

你会建议什么?

1 个答案:

答案 0 :(得分:1)

鉴于你想匹配字母数字字符和一些特殊符号#include <device_launch_parameters.h> // threadIdx #include <stdio.h> // fprintf #include <math.h> // ceil #include "simple_math.cuh" cudaError_t mathWithCuda(double *c, const float *a, const float *b, unsigned int arraySize, int mode) { float *dev_a, *dev_b; double *dev_c; cudaError_t cudaStatus; cudaStatus = cudaSetDevice(0); if (cudaStatus != cudaSuccess) { fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?"); goto Error; } if ((cudaStatus = cudaMalloc((void**)&dev_c, arraySize * sizeof(double))) != cudaSuccess || (cudaStatus = cudaMalloc((void**)&dev_a, arraySize * sizeof(float))) != cudaSuccess || (cudaStatus = cudaMalloc((void**)&dev_b, arraySize * sizeof(float))) != cudaSuccess) { fprintf(stderr, "cudaMalloc failed!"); goto Error; } if ((cudaStatus = cudaMemcpy(dev_a, a, arraySize * sizeof(float), cudaMemcpyHostToDevice)) != cudaSuccess || (cudaStatus = cudaMemcpy(dev_b, b, arraySize * sizeof(float), cudaMemcpyHostToDevice)) != cudaSuccess) { fprintf(stderr, "cudaMemcpy failed!"); goto Error; } int blocksPerGrid, threadsPerBlock; if (arraySize < 512) { blocksPerGrid = 1; threadsPerBlock = arraySize; } else { blocksPerGrid = ceil(double(arraySize) / double(threadsPerBlock)); threadsPerBlock = 512; } switch (mode) { case 0: addKernel <<<blocksPerGrid, threadsPerBlock >>>(dev_c, dev_a, dev_b); break; case 1: subKernel <<<blocksPerGrid, threadsPerBlock >>>(dev_c, dev_a, dev_b); break; case 2: mulKernel <<<blocksPerGrid, threadsPerBlock >>>(dev_c, dev_a, dev_b); break; case 3: divKernel <<<blocksPerGrid, threadsPerBlock >>>(dev_c, dev_a, dev_b); break; default: // nothing break; } cudaStatus = cudaGetLastError(); if (cudaStatus != cudaSuccess) { fprintf(stderr, "Kernel launch failed: %s\n", cudaGetErrorString(cudaStatus)); goto Error; } cudaStatus = cudaDeviceSynchronize(); if (cudaStatus != cudaSuccess) { fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching Kernel!\n", cudaStatus); goto Error; } cudaStatus = cudaMemcpy(c, dev_c, arraySize * sizeof(float), cudaMemcpyDeviceToHost); if (cudaStatus != cudaSuccess) { fprintf(stderr, "cudaMemcpy failed!"); goto Error; } Error: cudaFree(dev_c); cudaFree(dev_a); cudaFree(dev_b); return cudaStatus; } __global__ void addKernel(double *c, const float *a, const float *b) { int i = threadIdx.x; c[i] = __fadd_rn(a[i], b[i]); // a + b } __global__ void subKernel(double *c, const float *a, const float *b) { int i = threadIdx.x; c[i] = __fsub_rn(a[i], b[i]); // a - b } __global__ void mulKernel(double *c, const float *a, const float *b) { int i = threadIdx.x; c[i] = __fmul_rn(a[i], b[i]); // a * b } __global__ void divKernel(double *c, const float *a, const float *b) { int i = threadIdx.x; c[i] = __fdividef(a[i], b[i]); // a/b } 你必须在像这样的字符类中提及它们。

正则表达式: ()-_,./

<强>解释

^(?!.*([(),.#/-])\1)([\w (),.#/-]+)$断言不应该在字符类中提到多个字符。这从字符串的开头断言。

(?!.*([(),.#/-])\1)匹配字符串的其余部分,从头到尾包含允许的字符。

Regex101 Demo