Tag: regex

正确匹配Java字符串文字

我正在寻找一个正则表达式来匹配Java源代码中的字符串文字。 可能吗? private String Foo = “A potato”; private String Bar = “A \”car\””; 我的意图是用其他东西替换另一个字符串中的所有字符串。 使用: String A = “I went to the store to buy a \”coke\””; String B = A.replaceAll(REGEX,”Pepsi”); 像这样的东西。

从字符串中删除Dollar和逗号

我们如何从相同的字符串中删除美元符号($)和所有逗号(,)? 避免使用正则表达式会更好吗? String liveprice = “$123,456.78”;

如何在正则表达式中表示双引号字符(“)?

我需要使用正则表达式来检查字符串是否以双引号字符( ” )开头,并以双引号字符结尾。 问题是我不能使用双引号字符,因为它会混淆。 有没有其他方法来代表双引号字符”在正则表达式中,或在字符串中一般? String s = “””; // ?

在Java中使用regex for switch-statement

void menu() { print(); Scanner input = new Scanner( System.in ); while(true) { String s = input.next(); switch (s) { case “m”: print(); continue; case “s”: stat(); break; case “[AZ]{1}[az]{2}\\d{1,}”: filminfo( s ); break; case “Jur1”: filminfo(s); break; //For debugging – this worked fine case “q”: ; return; } } } 好像我的正则表达式已关闭或我在case语句中没有正确使用它。 我想要的是一个字符串:开头只有一个大写字母,后面跟着两个小写字母,后跟至少一个数字。 我已经检查了正则表达式API并尝试了三种变体(贪婪,不情愿和占有量词)而不知道它们的正确用法。 还检查了String的方法,但未找到与我的需求相关的方法。

如何从java字符串中删除控制字符?

我有一个来自UI的字符串,可能包含控制字符,我想删除除回车符 , 换行符和制表符之外的所有控制字符。 现在我可以找到两种方法来删除所有控制字符: 1-使用番石榴: return CharMatcher.JAVA_ISO_CONTROL.removeFrom(string); 2-使用正则表达式: return string.replaceAll(“\\p{Cntrl}”, “”);

将正则表达式应用于Java I / O流

我寻找一个将正则表达式应用于Java I / O流的示例,该流不会简单地将流转换为字符串,因为我希望保留二进制数据。 互联网上的大多数例子都集中在文本数据上……

如何处理包含正斜杠(/)的请求?

我需要处理如下请求: www.example.com/show/abcd/efg?name=alex&family=moore (does not work) www.example.com/show/abcdefg?name=alex&family=moore (works) www.example.com/show/abcd-efg?name=alex&family=moore (works) 它应该接受来自www.example.com/show/和?之间的值的任何类型的字符? 。 请注意,那里的值将是单个值而不是操作的名称。 例如: /show/abcd/efg和/show/lkikf?name=Jack ,其中第一个请求应该将用户重定向到页面abcd/efg (因为这是一个名称),第二个请求应该将用户重定向到页面lkikf具有参数名称的值。 我有跟随控制器来处理它,但问题是当我有/在地址控制器无法处理它。 @RequestMapping(value = “/{mystring:.*}”, method = RequestMethod.GET) public String handleReqShow( @PathVariable String mystring, @RequestParam(required = false) String name, @RequestParam(required = false) String family, Model model) { 我使用了以下正则表达式,但没有用。 /^[ A-Za-z0-9_@./#&+-]*$/

需要在java中将字符串拆分为两部分

我有一个字符串,其中包含一个连续的数字块,然后是一个连续的字符块。 我需要将它们分成两部分(一个整数部分和一个字符串)。 我尝试使用String.split(“\\D”, 1) ,但它吃掉了第一个字符。 我检查了所有的String API,但没有找到合适的方法。 做这件事有什么方法吗?

在Java中查找字符串中出现的所有子字符串

我试图在Java中查找字符串中所有出现的子字符串。 例如:搜索“ababsdfasdfhelloasdf”为“asdf”将返回[8,17],因为有2个“asdf”,一个位于8位,一个位于17位。搜索“aaaaaa”中的“aa”将返回[0,1, 1,2,3,4]因为在位置0,1,2,3和4处有一个“aa”。 我试过这个: public List findSubstrings(String inwords, String inword) { String copyOfWords = inwords; List indicesOfWord = new ArrayList(); int currentStartIndex = niwords.indexOf(inword); int indexat = 0; System.out.println(currentStartIndex); while (cthing1 > 0) { indicesOfWord.add(currentStartIndex+indexat); System.out.println(currentStartIndex); System.out.println(indicesOfWord); indexat += cthing1; copyOfWords = copyOfWords.substring(cthing1); System.out.println(copyOfWords); cthing1 = copyOfWords.indexOf(inword); } 这个问题可以在Python中解决如下: indices = [m.start() for m in […]

在Java中用’\\’替换单个’\’

如何用’\\’替换单个’\’ ‘\\’ ? 当我运行replaceAll()然后我收到此错误消息。 Exception in thread “main” java.util.regex.PatternSyntaxException: Unexpected internal error near index 1 \ ^ at java.util.regex.Pattern.error(Pattern.java:1713) at java.util.regex.Pattern.compile(Pattern.java:1466) at java.util.regex.Pattern.(Pattern.java:1133) at java.util.regex.Pattern.compile(Pattern.java:823) at java.lang.String.replaceAll(String.java:2190) at NewClass.main(NewClass.java:13) Java Result: 1 我的代码: public class NewClass { public static void main(String[] args) { String str = “C:\\Documents and Settings\\HUSAIN\\My Documents\\My Palettes”; str = str.replaceAll(“\\”, “\\\\”); […]