Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare "anything" using java?

I'm trying to find and read a string from a text file using a ".equal" in java. I know that I can simply do this by using ".contain" instead, but I just want to know if there is anyway to find and read the line like this:

For example I have the following data in the text file:

ID1   123   123
ID2   125   136

Now

String ID="ID1"; 
while ((ss = buffered_reader.readLine()) != null){
if (ss.equals(ID+[anything])){do something} } 

Why I want to this is to prevent getting result for entering just "ID". So is there any way like a regex or something that can help ?

Clarification :

I want to find the exact match for the ID , for example :

if you enter ID1 then the program says : true but false for entering just ID

if I use .contain the program finds the word ID1 or ID2 and because they contain "ID" , then it always returns a true value.

now I want to use .eqaual to find the exact match followed by anything

like image 578
Loop Masters Avatar asked Aug 16 '26 09:08

Loop Masters


2 Answers

Try this:

String ID="ID1"; 
while ((ss = buffered_reader.readLine()) != null) {
    if (ss.matches(ID+".*")){do something} }
}

The expresion .* means any sequence of characters, even an empty sequence. So if ID is "ID1" then it will match any string that begins with ID1.

like image 146
tibtof Avatar answered Aug 19 '26 00:08

tibtof


Try this regex:

ID.+

This will only match if the string has "ID" substring plus one character at least.

like image 32
m0skit0 Avatar answered Aug 18 '26 22:08

m0skit0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!