简体   繁体   English

正则表达式模式匹配在java中不起作用

[英]Regex pattern matching doesn't work specifc string in java

I was using a REGEX pattern in java (given below):我在 java 中使用了 REGEX 模式(如下所示):工作模式

for the string:对于字符串:工作字符串 It works fine.它工作正常。 But when I tried using the below pattern:但是当我尝试使用以下模式时:非工作模式

for the string:str =对于字符串:str =

非工作字符串 Sorry about the image upload.抱歉上传图片。 Looks like the character '[]' in a00[] is encoded differently on the browser.看起来 a00[] 中的字符 '[]' 在浏览器上的编码方式不同。 Any ways to read that character in a different manner?有什么方法可以以不同的方式阅读该角色? The same character has a different representation in notepad++.同一个字符在记事本++中有不同的表示。 I'm using RXTX and inputStream.read(readBuffer) to read the data.我正在使用 RXTX 和 inputStream.read(readBuffer) 来读取数据。 Is there any way I can update my encoding methods in java to overcome this?有什么方法可以更新我在java中的编码方法来克服这个问题? http://i.imgur.com/sdUjS.jpg i.imgur.com http://i.imgur.com/sdUjS.jpg i.imgur.com

PS: Sorry about the image description - if it type it out i cant represent that character. PS:对图像描述感到抱歉 - 如果它输入它我不能代表那个角色。 when i copy paste that character, it becomes an empty space.当我复制粘贴那个字符时,它变成了一个空白区域。

The strange symbol (└) looks like how ASCII 3 is represented in some fonts.奇怪的符号 (└) 看起来像 ASCII 3 在某些字体中的表示方式。

In Regex, \\b matches a word boundary.在正则表达式中, \\b匹配单词边界。 That is, between an alphanumeric and non-alphanumeric character.即,介于字母数字字符和非字母数字字符之间。 It works in the first case because there is a digit ("9") before the matched substring, and an exclamation mark ("!") right after it (which is a non-alphanumeric character).它适用于第一种情况,因为在匹配的子字符串之前有一个数字(“9”),在它(非字母数字字符)之后有一个感叹号(“!”)。

In the second case you changed the exclamation mark to a letter, so there is no longer a transition from alphanumeric to non-alphanumeric.在第二种情况下,您将感叹号更改为字母,因此不再存在从字母数字到非字母数字的转换。

The solution is to extend the Regex so it also matches the symbol and digit:解决方案是扩展正则表达式,使其也匹配符号和数字:

Pattern.compile("(\\x03\\d)(a)\\w*(?=\\x03\\d)");

I used \\\\x03\\\\d to match the codes.我使用\\\\x03\\\\d来匹配代码。 The last part (?= ) is a look-ahead.最后一部分(?= )是前瞻。 It checks if it matches, but does not consume it.它检查它是否匹配,但不消耗它。 This is so, so you do multiple matches in a row.就是这样,因此您可以连续进行多个匹配。


A simpler alternative, would be to just split the string on "└", and examine the pieces.一个更简单的替代方法是在“└”上拆分字符串,然后检查各个部分。

s.split("\u0003")

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

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