funky but simple regex

I

iksrazal

I'm having a hard time combining java string literals, character
escapes newline/carriage returns and regular expressions.

I have a situation where on a particular socket call this string
indicates EOF: Using linux od -c :

0000000 ( P D T ) \r \n # \n
0000011

So the PDT can change. I'd like to match on ') \r \n # \n'

Can someone help me get this code to work? I'm trying to match from
end of line ') \r \n # \n'

import java.util.*;
import java.io.*;
import java.util.regex.*;

/**
*/
class Reg
{
public static void main(String args[])
{
try
{
Reg rn = new Reg();
rn.doWork();
System.out.println("\n\nDone");
}
catch (Exception e)
{
e.printStackTrace();
}
}

public void doWork()
{
try
{
CharSequence inputStr = "any_garbage_here)\r\n#\n";
String patternStr = "\\(\\#$";

// Compile with dotall enabled
Pattern pattern = Pattern.compile(patternStr, Pattern.DOTALL);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.matches();
if (matchFound)
{
System.out.println("\n\nMatch!");
}
else
{
System.out.println("\n\nNO Match!");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

iksrazal
 
R

Roedy Green

I'm having a hard time combining java string literals, character
escapes newline/carriage returns and regular expressions.

See the quoter amanuensis. It will convert your regexes quoting all
chars that might need it. You undo those which are really commands,
and then feed them in and ask for Java string quoting. Or just use
the last step.

see http://mindprod.com/quoter.html
 
T

Thomas Schodt

iksrazal said:
... this string
indicates EOF: Using linux od -c :

0000000 ( P D T ) \r \n # \n
0000011

So the PDT can change. I'd like to match on ') \r \n # \n'
CharSequence inputStr = "any_garbage_here)\r\n#\n";
String patternStr = "\\(\\#$";

// Compile with dotall enabled
Pattern pattern = Pattern.compile(patternStr, Pattern.DOTALL);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.matches();

Matcher.matches() returns true only if the entire inputStr matches.

You need to match the garbage (PDT) part (".+")
and you need to match ")\r\n#\n",
you don't need the $ at the end.

String patternStr = ".+\\)\r\n#\n";
 
I

iksrazal

Thomas Schodt said:
Matcher.matches() returns true only if the entire inputStr matches.

You need to match the garbage (PDT) part (".+")
and you need to match ")\r\n#\n",
you don't need the $ at the end.

String patternStr = ".+\\)\r\n#\n";

That worked, thanks!
iksrazal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top