Link to home
Create AccountLog in
Avatar of EmadGirgis
EmadGirgis

asked on

Java regular expressions...

Hi,

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.
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

str.replaceAll("(\\n+|\\-\\-)", "\\s");
or
str.replaceAll("\\n+", "\\s");
str.replaceAll("\\-\\-", "\\s");
Avatar of EmadGirgis
EmadGirgis

ASKER

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

CHEJ,

Here is a text example

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

I need to get rid of -- This select statement would get active products and
 --Uniqe id
Is your solution is still valid based on the example I gave
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
<<What are the imports that I need to have>>
none

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

<<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
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
:)