简体   繁体   中英

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.

Use a lookahead:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM