Reputation: 1575
I have a text like (:year)
i want to extract the year and replace it with 1.
I am using
replaceAll("\\:[a-z A-Z]+?($|\\W)", "1");
This gives me the answer(1. It removes the closing bracket. What change should i make to exclude this closing bracket.
Input may be like (:year), :year , :year xxxx, and :year may be at the end of string. Using this in JAVA
Upvotes: 1
Views: 58
Reputation: 67968
replaceAll("\\:[a-zA-Z]+\\b", "1");
Simply use \b
or word boundary
.See demo.
https://regex101.com/r/uF4oY4/71
Upvotes: 4
Reputation: 174696
Turn the second group as positive lookahead. Positive lookahead are assertions which won't capture any characters but asserts whether a match is possible or not.
string.replaceAll("\\:[a-z A-Z]+?(?=$|\\W)", "1");
Upvotes: 1