简体   繁体   English

正则表达式:数字后跟一个字符

[英]Regex : Digits followed by only one character

I'm trying to create a regex that will allow only digits followed by only one character after every digit within a Textfield 我正在尝试创建一个正则表达式,它只允许在Textfield中的每个数字后面只有一个数字后跟一个字符

Regex that needs to match - \\d*\\+{1} 需要匹配的正则表达式 - \\ d * \\ + {1}

Regex in case it does not match - [^\\d*\\+){1}] will replace with "" (removes everything else) 正则表达式,如果它不匹配 - [^ \\ d * \\ +){1}]将替换为“”(删除其他所有)

final String regexFinalInteger = "\\d*\\+{1}";

    numberElements.textProperty().addListener((observable, oldValueE, newValueE) -> {
        if (!newValueE.matches(regexFinalInteger)) {
            numberElements.setText(newValueE.replaceAll("[^\\d*\\+){1}]", ""));
        }
    });

I will expect an output of 122+1+3 but the actual output can be 1++2+++4+123 (multiple +) 我期望输出122 + 1 + 3,但实际输出可以是1 ++ 2 +++ 4 + 123(多个+)

If I understood correctly, you want to replace multiple + s with only one. 如果我理解正确,您只想用一个替换多个+

I believe this would enable what you're looking for: 我相信这可以满足您的需求:

String regex = "[+](?=[+])";

String text = "122+1+3";
assertEquals("122+1+3", text.replaceAll(regex, ""));

text = "1++2+++4+123";
assertEquals("1+2+4+123", text.replaceAll(regex, ""));

That is my first Java program, I'm sorry if it offends someone. 这是我的第一个Java程序,如果它冒犯了某人,我很抱歉。

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

相关问题 正则表达式匹配空格后跟一个或多个字符 - Regex to match space followed by one or more character 正则表达式允许一两位数字后跟逗号(也允许空格) - Regex to allow one or two digits followed by commas (spaces are allowed too) RegEx 模式两个字母或数字后跟数字 - RegEx pattern two letters or digits followed by digits 正则表达式匹配重复模式,一个字符后跟一个空格 - Regex matching repeated pattern, one character followed by a whitespace 正则表达式 - Select 只有一个字符,如果它后面跟着另一个字符 - Regular Expression - Select only one character if it's followed by another char 正则表达式用于匹配数字,后跟特定的模式 - RegEx for matching digits not followed by specific pattern 正则表达式:匹配通配符,后跟可变长度的数字 - Regex: Match wildcard followed by variable length of digits Java正则表达式查找两个大写字母,后跟一个空格和七个数字 - Java regex to find two upper case letters, followed by one space and seven digits 什么正则表达式将匹配4位数,一个句点,然后一个,只有一个数字? - What regex will match 4 digits, a period, then one and only one digit? 正则表达式在java中删除一个字符,如果后面跟着另一个字符 - Regex to remove a character if followed by another character in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM