简体   繁体   English

正则表达式-解析字符串

[英]Regex - parsing string

Say i have a file which contains lines as follows: 说我有一个文件,其中包含以下行:

Hayden
y

Suppose i want to manipulate the line which only contains "y" and not the one with Hayden, how would i do this? 假设我要操纵仅包含“ y”的行,而不要包含与Hayden的行,我该怎么做?

So i read in the file and parse it line by line. 所以我读了文件并逐行解析。 I want to say if the line contains letters before or after "y" then it's not the line i'm looking for. 我想说的是,如果该行在“ y”之前或之后包含字母,那么这不是我要查找的行。

I thought i could do the following: 我以为我可以做到以下几点:

String value = "y"; 
if(strLine.matches("[a-zA-Z]" + value + "[a-zA-Z]"))
{
      don't manipulate line here
}
else
{
   manipulate string here
}

However, this gets "Hayden" as well as "y" 但是,这将得到“ Hayden”和“ y”

Any ideas? 有任何想法吗?

EDIT 编辑

sorry, i should have been more clear, what if i don't mind if there are spaces or symbols in front? 抱歉,我应该更清楚一些,如果我不介意前面是否有空格或符号怎么办? or behind? 还是落后? it's specifically the letters that i need to watch out for. 特别是我需要注意的字母。 For instance, i can't have yh but i can have y=... sorry again 例如,我不能拥有yh,但是我可以拥有y = ...再次抱歉

You can use negative lookarounds : 您可以使用否定性环顾

if (strLine.matches("^.*(?<![a-zA-Z])y(?![a-zA-Z]).*$")) { 
    // manipulate string here
}

The anchors are optional but included anyway for clarity. 锚点是可选的,但为了清楚起见都包括在内。

You can use: 您可以使用:

strLine.matches("^y$")

To ignore symbols, ie non-alphanumeric characters, use: 要忽略符号(即非字母数字字符),请使用:

strLine.matches("^\\W*y\\W*$")

Maybe this is what you are looking for 也许这就是您要寻找的

String[] lines={"Hayden","y"," y*","y=","y+"," +y..."};
for (String s:lines)
    System.out.println(s+"->"+s.matches("\\W*y\\W*"));

output: 输出:

Hayden->false
y->true
 y*->true
y=->true
y+->true
 +y...->true

If you are going to use regex, you need to be a bit more specific: 如果要使用正则表达式,则需要更具体一些:

  • y matches a y anywhere in the line. y匹配y在该行的任何地方。
  • ^y$ matches a y that is right before the end of the string (the dollar sign) and right after the beginning of the string (the caret). ^y$与一个y匹配,该y位于字符串结尾(美元符号)之前,并且恰好位于字符串开头(插入符号)之后。 This lets you match the line that is equal to y . 这使您可以匹配等于 y的线。

Extending the equals approach: Just remove all the character you don't mind, and then check for equality with "y" : 扩展equals方法:删除所有您不介意的字符,然后使用"y"检查是否相等:

if (strLine.replaceAll("[^a-zA-Z]", "").equals("y")) {
    ...
}

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

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