1

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?

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.

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"

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

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

13 Comments

this is probably the easiest way to show if the line only contains y (and you can also use "equalsIgnoreCase()"
sorry, i should have been more clear, what if i don't mind if there are symbols? or spaces in front?
this almost works... but for some reason it's not catching y= y+y... hwo would i get around this? as i said, i don't mind there being symbols or spaces... just can't have letters right beside it.. thanks for the response btw
@MarkByers sorry, again.. i should have been more clear, i mean right next to "y" there shouldn't be any letters, but any symbol is ok
@BlueMonster: When you say any letter, do you really mean any letter? Because in your question you write A-Z but there are letters such as Ø that aren't included in A-Z.
|
3

You can use:

strLine.matches("^y$")

To ignore symbols, i.e. non-alphanumeric characters, use:

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

1 Comment

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
1

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

2 Comments

hmm but what about having something like y= or y+ or +y... i think this would fail in that case... but these lines are ok for me. it's just if there is a letter directly beside y - on either side, that i want to avoid..
thanks :) this actually worked. pretty cool stuff man. Thanks again
0

If you are going to use regex, you need to be a bit more specific:

  • y matches a y anywhere in the line.
  • ^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). This lets you match the line that is equal to y.

Comments

0

Extending the equals approach: Just remove all the character you don't mind, and then check for equality with "y":

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

3 Comments

i can't remove the letters i don't want... they must stay there... i just have to work around them... because i don't want to manipulate those lines...
replace() does not manipulate anything, it returns a new string. The condition is to read "if I were to take out everything that is not a-Z and the rest then is just a "y", do..."
but i don't want to do anything with the line that contains other letters.... only if the line contains the letter i'm looking for and symbols or spaces... in such a case, i would like to manipulate the string. However, if i remove everything and then check if there is a "y" how do i determine that this is or is not a line containing just the letter i'm looking for and symbols (if any) - although there does not necessarily have to be any symbols...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.