Java regular expressions...


I have string. The string has multiple lines in it.. I mean it contain \n.

Now I need to replace every occurrence of -- and any number of letters and  \n by a space
again the pattern is
minus minus followed by any number of characters and ending by new line
this pattern needs to be replace by a single space

I am new for regular expressions and java.

Can you please point to me also which packages I need to import. I hope you provide an example. You can call the String str or something.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gurvinder Pal SinghCommented:
str.replaceAll("(\\n+|\\-\\-)", "\\s");
str.replaceAll("\\n+", "\\s");
str.replaceAll("\\-\\-", "\\s");
EmadGirgisAuthor Commented:
str.replaceAll("(\\n+|\\-\\-)", "\\s");

My issue is the -- comes before the newline
What here represents zero or many characters between both

is the \\s replaces that with a space.

What are the imports that I need to have

What type is str
is this regular string
Not entirely clear what you want to match (would have been good to post an example) but try
s = s.replaceAll("--\\w*?\n", " ");

Open in new window

Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

EmadGirgisAuthor Commented:

Here is a text example

-- This select statement would get active products
Product_ID ,  --Uniqe id
from  product where product_active = 1;

I need to get rid of -- This select statement would get active products and
 --Uniqe id
EmadGirgisAuthor Commented:
Is your solution is still valid based on the example I gave
In that case, try simply
s = s.replaceAll("--.*?\n", " ");

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gurvinder Pal SinghCommented:
<<What are the imports that I need to have>>

<<What type is str
is this regular string>>

<<My issue is the -- comes before the newline
What here represents zero or many characters between both>>

Actually order doesn't matter. I have given '|' in the regular expression, which means whenever either of \\n or \\-\\- comes, it will be replaced by space \\s
Gurvinder Pal SinghCommented:
actually it needs to be

str.replaceAll("(\\n+|\\-\\-)", " ");
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.