AlvinfromDiaspar
AlvinfromDiaspar

Reputation: 6822

RegEx to exclude a prefix string?

How to write a regular expression that excludes a particular prefix string?

Example input: "Hello World - How are you today?"

Desired output: "How are you today?"

Prefix strings to exclude: "Hello world - "

Upvotes: 6

Views: 7999

Answers (4)

paulie.jvenuez
paulie.jvenuez

Reputation: 295

What about.

(?<=[\w -])[\w\W]+

Upvotes: 0

SQB
SQB

Reputation: 4078

Sounds to me like you want to replace, so s/^Hello World - (.*)/$1/. If you just want to match, use (?<=^Hello World - ).*. Both regexes assume you only want to exclude Hello World - if it's at the start of a line.

Upvotes: 0

Keith Nicholas
Keith Nicholas

Reputation: 44288

this will match everything after,

(?<=Hello\sWorld\s-\s).*

Upvotes: 4

Xiao Jia
Xiao Jia

Reputation: 4259

How about lookbehind? i.e. (?<=Hello World - )How are you today\?

A drawback is in (?<=xxx) the xxx must be fixed length (at least in most implementations that I am aware of).

Upvotes: 3

Related Questions