Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Regular Expression for carriage return occuring at begining or end file.

Last post 12-24-2009, 3:36 PM by Sergei Z. 3 replies.
Sort Posts: Previous Next
  •  12-24-2009, 1:33 PM 58070

    Regular Expression for carriage return occuring at begining or end file.

    Regular Expression for carriage return occuring at begining or end file.

    \r\n <-- remove this guy
    some stuff to say \r\n
    some more stuff to say \r\n
    \r\n <-- remove this guy

    I want to write regex but do not work. I try with

    ^(\r\n)+|\r\n(\r\n)+$
    and
    ^\r\n|\r\n$

    Can smeone please tell me what i am doing wrong?

    Thx
  •  12-24-2009, 2:10 PM 58071 in reply to 58070

    Re: Regular Expression for carriage return occuring at begining or end file.

    What OP are you using?

    Personally i would not approach this task through regexp. The following is a PERL way of achieving what you want, however most OP's will be able to achieve similar results, just will different function calls.

    1. Read in the file line by line and dump it into an array
    2. Obtain array length so you can grab the first and last element
    3. use CHOMP on both to strip it of all \r\n in that line.
    4. Re-add the \r\n at the end of the first line (Assuming it isnt just a blank line).
    5. print the array to file making whatever line by line regex substitutions you had in mind.

    Now some people will claim that this is more time consumming, however in most cases you have to read the file in anyway. The time difference between a chomp+string edit and a regex substitution is negligible.

    If you are set however on using a regex, (once again can't really help with that unless you tell us the OP or regex variant you are using). Then depending on how you are reading the file you have 2 options. If you are reading line by line, then you need to implement a line counter and activate the \r\n removing regex only on the first and last line. If you are doing a multiline read then use the /m and /g option for your regex with this: ^\s*|\s*$ regex.

    The reason i stated \s rather than \r\n is due to the differences in systems/software/character sets. My files for example rarely have \r\n in then. Rather they only use \n. This difference can also cause problems with regex. If \r\n is required then just go back to your original regex with \m \g options set

  •  12-24-2009, 2:26 PM 58072 in reply to 58071

    Re: Regular Expression for carriage return occuring at begining or end file.

    Hi and thx for so fast answer. In perl this works nice yes but i need that regex for C#.

    I can do with reading line by line but there is so few text that regex is better for my opinion.

     

    So what i want to do.

     

    For example this is text:

    ----

    \r\n\r\n Lorem Ipsum is simply dummy text of the printing and typesetting industry. \r\n\r\nLorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \r\n\r\nIt has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. \r\n\r\nIt was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\r\n\r\n

    ---

    Now i want: First and last \r\n\r\n are replaced with "", and middle on with " "

    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

     

    regards

     

  •  12-24-2009, 3:36 PM 58073 in reply to 58072

    Re: Regular Expression for carriage return occuring at begining or end file.

    can you just replace [C#]

    (\r\n)+

    with

    <white space>

    and then delete one whitespace at the start and the end of the resulting string:

     Raw Match Pattern:
    (\r\n)+

    Raw Replace Pattern:


    Source text after replacement:
     Lorem Ipsum is simply dummy text of the printing and typesetting industry.  Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.  It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.  It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. ---  

     

View as RSS news feed in XML