Jagan
Jagan

Reputation: 620

Regex to replace a word by starting sub String and ends with dot or space in Java

I need to replace #id in my css String, let say I have id as #id_123456 as id in my css I have to replace it with #id_7891011. I know my id starts with #id_ and it can end with ./space/+/>. I am trying to do regex to replace all my old id with new Id. My regex looks like.

(.* ?)(#id_[a-zA-Z0-9 ]+[.^\S])(.*)

It working for the start condition but not end by the space or dot it replaces the whole line. Any help would be appreciated.

Upvotes: 0

Views: 618

Answers (1)

Ryszard Czech
Ryszard Czech

Reputation: 18611

Use a lookahead:

#id_[a-zA-Z0-9\s]+(?=[.\s+>])
                  |_________|

See proof. The (?=[.\s+>]) will make sure matching stops before ./whitespace/+/>.

Upvotes: 1

Related Questions