ashwini
ashwini

Reputation: 531

how to parse a string for excluding special characters with regex

I have one string for ex. "Drive C: Disk Usage". Everything after the ":" is a text I want to extract with regex. (here, its "Disk Usage") I am parsing it with this regex: :[^:].* but it considers ":" as well which i don't want . How can I change my regular expression so that it will match any text after ":" Please help.

Upvotes: 0

Views: 1066

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174706

Use a positive lookbehind,

(?<=:).*

It matches all the characters which are just after to :.

Explanation:

  • (?<=) Positive lookbehind.
  • .* Matches any character(except a newline character) zero or more times.

DEMO

OR

:\K[^:].*

Explanation:

  • : A literal :
  • \K Used to discard previously matched characters.(ie; :)
  • .* Any character(except a newline character) zero or more times.

DEMO

Upvotes: 2

Jorge Faianca
Jorge Faianca

Reputation: 791

Some options

Positive lookbehind

(?<=:).*

Negative ( this should be faster )

[^:]*$

Matches everything after a : , it's after because we are using the end delimiter.

Upvotes: -1

Related Questions