This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes regex question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "regex question" Watch "regex question" New topic
Author

regex question

joe nesbitt
Greenhorn

Joined: Dec 05, 2009
Posts: 17
Hi all,

I have a file that contains sentences separated by CRLF.
Also in each sentence, each word is seperated by | and I need to replace the words which contains CRLF with space ("")...how do I do it using regex.

File content example:

aaa|bbbb|cccCRLF
zzz|yyyCRLFxxx|nnCRLF

I need to replace CRLF with "" in the second line only ( I need to ignore the CRLF that are at the end of each sentence).

Any help is highly appreciated.

Thanks in advance.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40


I am assuming that when you say "CRLF", you actually mean a "CRLF" string -- and not, the more common phrase used to refer to a carriage return line feed sequence.

BTW, what have you tried so far?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
joe nesbitt
Greenhorn

Joined: Dec 05, 2009
Posts: 17
When I refered to CRLF, I meant to say \r\n (return key usage) .

I tried stringxxx.replaceAll("\\r+.", "\\X000d ") for \r. But not sure how to replace only a particular \r I tried this but in vain:

stringxxx.replaceAll("|*\\r+.*|", "\\X000d ")


Any help is appreciated.

Thanks in advance.
Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213
you might try this

System.getProperty("line.separator")

instead of

\r\n


SCJP 6, SCWCD 5, SCBCD 5
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

joe nesbitt wrote:Hi all,

I have a file that contains sentences separated by CRLF.
Also in each sentence, each word is seperated by | and I need to replace the words which contains CRLF with space ("")...how do I do it using regex.

File content example:

aaa|bbbb|cccCRLF
zzz|yyyCRLFxxx|nnCRLF

I need to replace CRLF with "" in the second line only ( I need to ignore the CRLF that are at the end of each sentence).

Any help is highly appreciated.

Thanks in advance.



Am I missing something? I'm not sure this makes sense! You have shown two lines in your example but you have not said what constitutes a line. Since you are trying to replace \r\n you can't be using \r\n as a line separator. So what separates the lines and sentences in your file?

I don't see regex coming into a solution for this problem. If you are just wanting to replace ALL \r\n in a file by a single space then just read and write the file a char at a time (BufferedReader and BufferedWriter make this efficient) and look for \r\n and output a space when you find the \r\n pair.

Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

joe nesbitt wrote:I tried stringxxx.replaceAll("\\r+.", "\\X000d ") for \r. But not sure how to replace only a particular \r I tried this but in vain:

stringxxx.replaceAll("|*\\r+.*|", "\\X000d ")

String.replaceAll uses regular expressions. | has special meaning in regular expressions. Also, both your attempts would also remove the . (any character) / .* (all characters); that's not what you want, is it?

Check out the Javadoc of java.util.regex.Pattern and check for "positive lookahead" and "positive lookbehind".

However, I don't think using simple regular expressions will help you out here. How would your example be different from the following if you'd use only regular expressions: or even All represent the same characters. Is it really the number of | characters? If so then using a simple loop would probably be better. In pseudo code:
You'll probably want a StringBuilder to store the modified file contents.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
 
subject: regex question
 
Similar Threads
Regular Expression - replaceAll() - how to replace words?
cell formatting in excel using jsp
RegEx ! operator help
How to set width of cells in excel on browser ?
[regex] select word of 3 letter and more between other word