Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9140615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:32:22+00:00 2026-06-17T09:32:22+00:00

Having to use regex for the first time and although I almost achieved what

  • 0

Having to use regex for the first time and although I almost achieved what I require I do not seems to be able to combine into a single statement.

I have a string of words where I wish to replace \n if it is not preceded by a dot OR not preceded by a dot space.

I can run either of these two statements to achieve the required result. However, if I either run them one after another or try to combine them into a single regex, it does not work.

//replaces \n if not preceded by dot space
xx = xx.replaceAll("(.+)(?<!\\. )\n", "$1 ");


//replaces \n if not preceded by dot
xx = xx.replaceAll("(.+)(?<!\\.)\n", "$1 "); 


//one of my attempts to combine into a single statement
xx = xx.replaceAll("(.+)(?<!\\. )\n|(?<!\\.)\n", "$1 ");

Example of String I’m trying to fix.

BEFORE

This is some text which may\n
have a newline character to break the line\n
but I only want to remove it if it's not preceded with a full.\n
or it's not preceded with a full stop and a space. \n

AFTER

This is some text which may
have a newline character to break the line 
but I only want to remove it if it's not preceded with a full.\n
or it's not preceded with a full stop and a space. \n

I think I’m close, but being new to regex, I am getting more confused the more I read.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:32:23+00:00Added an answer on June 17, 2026 at 9:32 am

    It’s easier than you think:

    String resultString = subjectString.replaceAll("(?<!\\. ?)\n", " ");
    

    Explanation:

    (?<!   # Assert that the previous characters are not...
     \.    # a dot
     [ ]?  # optionally followed by a space
    )      # End of lookbehind
    \n     # Match a newline character
    

    So you don’t need to match (.+) in the first place, only to replace it with itself afterwards. Incidentally, here’s what tripped you up:

    (.+)(?<!\. )\n|(?<!\.)\n
    

    is logically grouped as

    (.+)(?<!\. )\n   # Match this
    |                # or
    (?<!\.)\n        # this
    

    so the (.+) is only matched if there is no space after the dot.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use regex expressions to catch some incoming urls, but I'm having
I'm trying to use the IIS 7 URL Rewrite feature for the first time,
I'm having some difficulty with a specific Regex I'm trying to use. I'm searching
I need to use pure Javascript for the first time in a long while,
I'm having a slight problem with regex I'm trying to use on my JTextArea
I have the misfortune of having use conio.h in vc++ 6 for a college
I know Apple is big on having you use NS objects instead of true
I am looking for the SQL syntax to use HAVING in the following statement:
I originally used JQuery for this project, but now I'm having to use Prototype,
I am having trouble to use the regular expression in between the unique element

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.