Regexp Octal question

J

julien

Hi,

Strange question, if somebody have an answer, will be really helpfull.

I used a regexp (octal) : < \0 > to find < (char)0 > char in a
string.

1) If i get the regexp \0 from a text file, the regexp NOT match.
2) If i defines directly the regexp \0 in java code, the regexp MATCH.
3) If i get regexp \x00 (another way to match (char)0) from a file,
the regexp MATCH
4) If i defines directly the regexp \x00 in java code, the regexp
MATCH

So, to resume, i cant successfull get the regexp from a file.
Any idea ? This case is really strange for me.
Thanks in advance.
 
H

hiwa

Hi,

Strange question, if somebody have an answer, will be really helpfull.

I used a regexp (octal) : < \0 > to find < (char)0 > char in a
string.

1) If i get the regexp \0 from a text file, the regexp NOT match.
2) If i defines directly the regexp \0 in java code, the regexp MATCH.
3) If i get regexp \x00 (another way to match (char)0) from a file,
the regexp MATCH
4) If i defines directly the regexp \x00 in java code, the regexp
MATCH

So, to resume, i cant successfull get the regexp from a file.
Any idea ? This case is really strange for me.
Thanks in advance.

I don't understand your question.
Post a small demo code that is generally compilable, runnable and
could reproduce your problem. See: http://homepage1.nifty.com/algafield/sscce.html
and http://www.yoda.arachsys.com/java/newsgroups.html
 
H

hiwa

I don't understand your question.
Post a small demo code that is generally compilable, runnable and
could reproduce your problem. See:http://homepage1.nifty.com/algafield/sscce.html
andhttp://www.yoda.arachsys.com/java/newsgroups.html

And there should be no problem. Try this code:
/*** content of the regex.txt file *************************
\0103
************************************************************/
import java.io.*;

public class Julien{

public static void main(String[] args) throws Exception{
String regexFromFile;
String regexFromCode = "\\0103";
String text = "ABCDEF";

BufferedReader br = new BufferedReader(new
FileReader("regex.txt"));

regexFromFile = br.readLine();

System.out.println(text.replaceAll(regexFromFile, "G"));
System.out.println(text.replaceAll(regexFromCode, "G"));
}
}
 
D

Daniele Futtorovic

Hi,

Strange question, if somebody have an answer, will be really helpfull.

I used a regexp (octal) : < \0 > to find < (char)0 > char in a
string.

1) If i get the regexp \0 from a text file, the regexp NOT match.
2) If i defines directly the regexp \0 in java code, the regexp MATCH.
3) If i get regexp \x00 (another way to match (char)0) from a file,
the regexp MATCH
4) If i defines directly the regexp \x00 in java code, the regexp
MATCH

So, to resume, i cant successfull get the regexp from a file.
Any idea ? This case is really strange for me.
Thanks in advance.

What mechanism are you using to read the file's content? Maybe it
interpretes the \0 as an END_OF_STRING marker.

BTW, if what you've described above is all your regex does, you might as
well go with String#indexOf(char).

Regards,
df.
 
J

julien

What mechanism are you using to read the file's content? Maybe it
interpretes the \0 as an END_OF_STRING marker.

BTW, if what you've described above is all your regex does, you might as
well go with String#indexOf(char).

Regards,
df.

Your right. I have made some test.
Then i get \0 from file, is not get like the special char \0 but like
"\" + "0".
If i put \00 instead of \0 in the file, the regexp works (\0 = \00)
So, i have a file with regexp \0, instead of change the file, i test
and replace all \0 found in the file with \00.
 
D

Daniele Futtorovic

Your right. I have made some test.
Then i get \0 from file, is not get like the special char \0 but like
"\" + "0".
If i put \00 instead of \0 in the file, the regexp works (\0 = \00)
So, i have a file with regexp \0, instead of change the file, i test
and replace all \0 found in the file with \00.

Come to think of it, I don't believe I'm right.
You didn't answer my question as to how you read the file's content, but
I cannot imagine any text reader actually thus interpreting the two
chars "\" and "0".
No, what's much more likely the explanation, IMO, is that the octal
notation is defined, roughly, as "any number, preceded by '\0'". In your
case, the number would be "". But that's NaN.
So, I think your assumption that "\0", as a regexp, ought to match the
NULL char is wrong. It'd match the char "0" (needlessly quoted), or it
may be interpreted as a backreference (not too sure), but it wouldn't
match the NULL character, and it seems to me as though this might be
called "working as designed" (and documented).

You said:
2) If i defines directly the regexp \0 in java code, the regexp MATCH.

Yes, that is, if you define the regex using "\0", as opposed to "\\0".
The "\0" gets interpreted by -- I don't know if its the compiler, just
like "\\" gets interpreted to "\". So in the end (the runtime), in your
regex pattern, there /is/ the character NULL, not a backslash and a
zero. But when you load the pattern String from a file, it transcedes
into the regex pattern /as is/.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top