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:

(?<=:).*
Share:
23,837
Aleks21
Author by

Aleks21

Updated on July 05, 2022

Comments

  • Aleks21
    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:

    1. It sent a notice of delivery of goods: UserName1
    2. Sent a notice of delivery of the goods: User is not found, UserName2
    3. It sent a notice of receipt of the goods: UserName1
    4. It sent a notice of receipt of the goods: User is not found, UserName2

    I want to get the text after colon

    1. UserName1
    2. User is not found, UserName2
    3. UserName1
    4. User is not found, UserName2
  • Dan Hastings
    Dan Hastings almost 7 years
    is there a way to do this so it excludes the colon? so for something:value, i would want it to return "value"
  • Ihor Dobrovolskyi
    Ihor Dobrovolskyi almost 7 years
    @Dan Hastings, did you try the code I've posted? It doesn't include a colon.
  • Dan Hastings
    Dan Hastings almost 7 years
    For me it returns the string with a colon at the start
  • Ihor Dobrovolskyi
    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.