• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

regex question

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
joe nesbitt
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you might try this

System.getProperty("line.separator")

instead of

\r\n
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic