简体   繁体   English

Java正则表达式找到第一个字符不在字符范围内

[英]Java regex find first char NOT in range of chars

In my SQL Server there is a PATINDEX function. 在我的SQL Server中有一个PATINDEX函数。 For example: 例如:

patindex('%[^A-Z,^a-z,^0-9,^\,^_]%', SqlInstanceName)

This returns the string index position of the first character that is NOT in AZ, NOT in az, NOT in 0-9, NOT = '\\' (backslash) and NOT = '_' (underscore). 这将返回第一个字符的string索引位置,该字符不在AZ中,不在az中,不在0-9中,NOT ='\\'(反斜杠)和NOT ='_'(下划线)。

I want to do the same in Java 1.6/1.7 using Regex or using any built-in Java library methods. 我想在Java 1.6 / 1.7中使用Regex或使用任何内置的Java库方法来做同样的事情。

In Java you can do: 在Java中,您可以:

Pattern p = Pattern.compile("[^\\w\\\\]");
Matcher m = p.matcher(input); // input is your String 

if (m.find())
   System.out.println(m.start()); // start position of your match

thanks all! 谢谢大家! Using above tips I wrote this Java code and it seems to work for me: 使用上面的提示我编写了这个Java代码,它似乎对我有用:

// start code fragment
Pattern pattern = Pattern.compile("([a-zA-Z0-9_\\\\]*)");
Matcher matcher = pattern.matcher(inputString);
if (matcher.find()) 
{
    inputString = matcher.group(1);
}
// end code fragment

Much appreciated! 非常感激!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM