繁体   English   中英

Java Regex X {n,m} X,至少n次但不超过m次

[英]Java Regex X{n,m} X, at least n but not more than m times

我正在尝试了解如何将电子邮件地址与以下模式匹配:

      myEmail@something.any

any应该在2,4个字符之间。

请在下面找到Java代码。 我不明白为什么它返回true。 谢谢!

public static void main(String[] args){

        String a = "daniel@gmail.com";
        String b = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.+-]+.[a-zA-Z]{2,4}";
        String c = "MyNameis1@abcx.comfff";
        Boolean b1 = c.matches(b);
        System.out.println(b1);
    }

输出:真

在正则表达式中. 匹配任何字符(换行符除外)。 如果要搭配. 从字面上看,您需要对其进行转义:

[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.+-]+\\.[a-zA-Z]{2,4}

这样比较好,但是仍然与MyNameis1@abcx.comf部分匹配。 我们可以添加一个字符串锚( $ )的末尾,以确保没有结尾的不匹配字符:

[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.+-]+\\.[a-zA-Z]{2,4}$

逃脱. String b = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.+-]+.[a-zA-Z]{2,4}"; 它是正则表达式中的特殊字符。

用法: String b = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.+-]+\\\\.[a-zA-Z]{2,4}";

在您的表情中,点表示任何字符。 转义并确保没有字符跟随您的最小/最大字符检查,如下所示:

[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.+-]+\\.[a-zA-Z]{2,4}$

在数组 x,y 中找到两个数字,其中 x <y, x repeats at least n 3 times and y 4 times< div><div id="text_translate"><p> 我一直在努力解决线性时间的数组问题,问题是:假设我们有一个数组 A [1...n] 编写一个返回 true 的算法,如果:数组 x,y 中有两个数字有以下内容:</p><ol><li> x < y</li><li> x 重复 n/3 次以上</li><li> y 重复 n/4 次以上</li></ol><p>我尝试编写以下 java 程序来执行此操作,假设我们有一个排序数组,但我认为这不是最好的实现。</p><pre> public static boolean solutionManma(){ int [] arr = {2,2,2,3,3,3}; int n = arr.length; int xCount = 1; int yCount = 1; int maxXcount= xCount,maxYCount = yCount; int currX = arr[0]; int currY = arr[n-1]; for(int i = 1; i < n-2;i++){ int right = arr[n-2-i+1]; int left = arr[i]; if(currX == left){ xCount++; } else{ maxXcount = Math.max(xCount,maxXcount); xCount = 1; currX = left; } if(currY == right){ yCount++; } else { maxYCount = Math.max(yCount,maxYCount); yCount = 1; currY = right; } } return (maxXcount > n/3 && maxYCount > n/4); }</pre><p> 如果有人对这类问题有算法想法(最好是 O(n)),我将不胜感激,因为我被这个问题困住了。</p></div></y,>

[英]Find two numbers in array x,y where x<y, x repeats at least n/3 times and y at least n/4 times

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 n边滚动m模x次 正则表达式正好 n 或 m 次 在数组 x,y 中找到两个数字,其中 x <y, x repeats at least n 3 times and y 4 times< div><div id="text_translate"><p> 我一直在努力解决线性时间的数组问题,问题是:假设我们有一个数组 A [1...n] 编写一个返回 true 的算法,如果:数组 x,y 中有两个数字有以下内容:</p><ol><li> x < y</li><li> x 重复 n/3 次以上</li><li> y 重复 n/4 次以上</li></ol><p>我尝试编写以下 java 程序来执行此操作,假设我们有一个排序数组,但我认为这不是最好的实现。</p><pre> public static boolean solutionManma(){ int [] arr = {2,2,2,3,3,3}; int n = arr.length; int xCount = 1; int yCount = 1; int maxXcount= xCount,maxYCount = yCount; int currX = arr[0]; int currY = arr[n-1]; for(int i = 1; i < n-2;i++){ int right = arr[n-2-i+1]; int left = arr[i]; if(currX == left){ xCount++; } else{ maxXcount = Math.max(xCount,maxXcount); xCount = 1; currX = left; } if(currY == right){ yCount++; } else { maxYCount = Math.max(yCount,maxYCount); yCount = 1; currY = right; } } return (maxXcount > n/3 && maxYCount > n/4); }</pre><p> 如果有人对这类问题有算法想法(最好是 O(n)),我将不胜感激,因为我被这个问题困住了。</p></div></y,> 在行首匹配{m,n} | {m,} | {,n} | {n}的正则表达式 Combitronics和质因数分解:计算(m,n)对的数量,其中GCD(m,n)= x 正则表达式{m,n}和(正则表达式){m,n}之间有什么区别? M x N矩阵中长度为K的最大得分路径 For-loop - 减少嵌套(nxm 操作) Java中更快的GCD(n,m)? 如果元素的出现次数超过 n 次,则删除该元素的出现次数 JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM