Regex to match a string after colon ":"
23,837
Solution 1
You can use the regex like this:
":(.*)"
Then, you should use something like this one (on Java):
Matcher m = Pattern.compile(":(.*)").matcher(text);
if (m.find())
{
System.out.println(m.group(1));
}
Solution 2
I think this is what you're looking for:
(?<=:).*
Author by
Aleks21
Updated on July 05, 2022Comments
-
Aleks21 almost 2 years
I need to identify a sub string from any string based on a regular expression.
For Example, take the following strings:
- It sent a notice of delivery of goods: UserName1
- Sent a notice of delivery of the goods: User is not found, UserName2
- It sent a notice of receipt of the goods: UserName1
- It sent a notice of receipt of the goods: User is not found, UserName2
I want to get the text after colon
- UserName1
- User is not found, UserName2
- UserName1
- User is not found, UserName2
-
Dan Hastings almost 7 yearsis there a way to do this so it excludes the colon? so for something:value, i would want it to return "value"
-
Ihor Dobrovolskyi almost 7 years@Dan Hastings, did you try the code I've posted? It doesn't include a colon.
-
Dan Hastings almost 7 yearsFor me it returns the string with a colon at the start
-
Ihor Dobrovolskyi almost 7 years@DanHastings But it can't. My example on Java works perfectly, so I can't tell you what you're doing wrong. You may ask a new question and provide the code that returns a string with a colon.