2015-03-18 74 views
-2

我需要帮助将等于代码转换为匹配(正则表达式)。非常感谢。将java代码转换为一个正则表达式

//string composed of prefix and suffix - Can be spaced 
    //PREFIX - alfa-numeric, variable length, upper/lower case 
    //SUFFIX - numeric, variable length, can be spaced 
    String str="xpto 123 456 789"; 

    String prefix_to_search="XPTO"; 
    String digits_to_search="123456789"; 

    //remove prefix (case insensitive) 
    str.substring(str.toUpperCase().startsWith(prefix_to_search) ? prefix_to_search.length() : 0). 
    //remove spaces 
    replace(" ",""). 
    equals(digits_to_search)); 

给定一个字符串 “xpto 123 456 789”

MATCH
“123456789”
“123 456 789”
“1 2 3 4 5 6 7 8 9”
“XPTO 123 456 789”
“XPTO 1 2 3 4 5 6 7 8 9”

NOT MATCH
“12345678”
“1234567890”
“XPTO 12345678”
“XPTO 1234567890”
“XP 123 456 789”
“123 456 789 XPTO”

换句话说:
- 前缀必须是IGNORED(case INSENSITVE!)
- 数字必须匹配(空格必须忽略!)

+0

请记住,你也可以在www.regexr.com上试试它 – ha9u63ar 2015-03-18 22:27:21

+0

为什么?如果你的代码有效,你是否真的需要改变它? – 2015-03-18 22:31:45

+0

它将应用于QUERY!我没有可用的所有字符串函数,但我可以使用“匹配”! – marcolopes 2015-03-18 22:36:16

回答

-1

这应该做到这一点(假设它是特定于这个问题)

(?i)\bxpto[0-9\W]+ 

你可以去检查它www.regexplanet.com

+0

@marcolopes,而不是说“它不工作”,请发布您的预期输出,以便我们能够理解你到底在期待什么。你被要求由其他用户这样做,你所说的“它不工作”.... – ha9u63ar 2015-03-18 23:07:59

1
(?i)(?:xpto)?\\s*1\\s*2\\s*3\\s*4\\s*5\\s*6\\s*7\\s*8\\s*9 

如果你想始终与前缀匹配过,去掉?前缀组之后(从你的更新看来,前缀并不是强制性的)