File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes [newbie] regex anomaly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[newbie] regex anomaly" Watch "[newbie] regex anomaly" New topic
Author

[newbie] regex anomaly

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

I'm trying to create a program that reads source files line by line (commonly .java source files that I've pasted from a book in .chm format), and, removes the line numbers so that I can compile them straight away.

NOTE: Disregard my 2nd post, this is because when I tried to edit my intial post, the option was not available and I hacked around it by using a quote.

I'm stuck at the regex part where I'm trying to match for:


1. import java.net.*;
2. import java.awt.*;
...
...


NOTE: The line number (in bold for emphasis) should be removed.


Jon
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

Update...

Jon Camilleri wrote:I'm trying to create a program that reads source files line by line (commonly .java source files that I've pasted from a book in .chm format), and, removes the line numbers so that I can compile them straight away.

I'm stuck at the regex part where I'm trying to match for:


1. import java.net.*;
2. import java.awt.*;
...
...


NOTE: The line number (in bold for emphasis) should be removed.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why not just use String's replaceFirst(regex, "") method and use (any number of digits followed by a period) as the regex?

(This is really just a sed script, I'm assuming you're doing it this way to get more Java practice in.)
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 272

When you define a regex in Java, you have to be careful about using backslashes. When you want to use a backslash as part of your regex, you have to use two backslashes in your Java string.

So this:
should be this:


SCJP 6 || SCWCD 5
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

David Newton wrote:Why not just use String's replaceFirst(regex, "") method and use (any number of digits followed by a period) as the regex?

(This is really just a sed script, I'm assuming you're doing it this way to get more Java practice in.)


The problem is finding the right regex pattern
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
if (Pattern.matches("[\d]\..", line)) //this is incorrect should include X+ ...
{//match?!}[/b]


The '\d' is already a character class, so you don't need to put it in square brackets. You might have been thinking of this:


However, matches() most likely won't work, since it's looking for an exact match. David's suggestion of using String's replaceFirst() method is probably what you really want.

John.
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

John de Michele wrote:
if (Pattern.matches("[\d]\..", line)) //this is incorrect should include X+ ...
{//match?!}[/b]


The '\d' is already a character class, so you don't need to put it in square brackets. You might have been thinking of this:


However, matches() most likely won't work, since it's looking for an exact match. David's suggestion of using String's replaceFirst() method is probably what you really want.

John.


It's a good idea actually thanks Now I'm getting an error indicating something wrong with the escape sequence



David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Backslashes ("\") in Java Strings are meaningful, and must be escaped.
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

David Newton wrote:Backslashes ("\") in Java Strings are meaningful, and must be escaped.

Thanks that's it
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

Jon Camilleri wrote:
David Newton wrote:Backslashes ("\") in Java Strings are meaningful, and must be escaped.

Thanks that's it


I'm wondering why the String.replaceAll is not replacing the string.


Output

1. //line numbers are not removed??
import
java.net.*;
2.
import
java.awt.*;
3.
import
java.awt.event.*;
4.
import
java.io.*;
5.
import
java.util.*;
6.
import
javax.naming.*;
7.
import
javax.naming.directory.*;
8.
import
javax.swing.*;
9.
10.
/**


[HENRY: Deleted tons of output that is probably not relevant]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18103
    
  39



The pattern is one of more number of digits, followed by a period, followed by another character (any character). And since you are using a scanner to extract the token, separated by whitespace, there isn't any characters after the period.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

Henry Wong wrote:

The pattern is one of more number of digits, followed by a period, followed by another character (any character). And since you are using a scanner to extract the token, separated by whitespace, there isn't any characters after the period.

Henry

Thanks

oops
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: [newbie] regex anomaly
 
Similar Threads
[newbie] bracketing error
java.lang.NoClassDefFoundError:
Pattern matching an email address read from a file
Using Scanner need help comparing two files so I can write out data I need.
[newbie] String.replace()/replaceAll() removed spaces