Mark
Mark

Reputation: 2689

Regex match everything after question mark?

I have a feed in Yahoo Pipes and want to match everything after a question mark.

So far I've figured out how to match the question mark using..

\?

Now just to match everything that is after/follows the question mark.

Upvotes: 204

Views: 679623

Answers (7)

Hossein Mohammadi
Hossein Mohammadi

Reputation: 1473

?(.*\n)+

With this you can get everything Even a new line

Upvotes: 11

Kadhem
Kadhem

Reputation: 11

str.replace(/^.+?\"|^.|\".+/, '');

This is sometimes bad to use when you wanna select what else to remove between "" and you cannot use it more than twice in one string. All it does is select whatever is not in between "" and replace it with nothing.

Even for me it is a bit confusing, but ill try to explain it. ^.+? (not anything OPTIONAL) till first " then | Or/stop (still researching what it really means) till/at ^. has selected nothing until before the 2nd " using (| stop/at). And select all that comes after with .+.

Upvotes: 0

DarkNeuron
DarkNeuron

Reputation: 8702

With the positive lookbehind technique:

(?<=\?).*

(We're searching for a text preceded by a question mark here)

Input: derpderp?mystring blahbeh
Output: mystring blahbeh

Example

Basically the ?<= is a group construct, that requires the escaped question-mark, before any match can be made.

They perform really well, but not all implementations support them.

Upvotes: 60

Mark Byers
Mark Byers

Reputation: 838326

Try this:

\?(.*)

The parentheses are a capturing group that you can use to extract the part of the string you are interested in.

If the string can contain new lines you may have to use the "dot all" modifier to allow the dot to match the new line character. Whether or not you have to do this, and how to do this, depends on the language you are using. It appears that you forgot to mention the programming language you are using in your question.

Another alternative that you can use if your language supports fixed width lookbehind assertions is:

(?<=\?).*

Upvotes: 108

s3v3n
s3v3n

Reputation: 8446

\?(.*)$

If you want to match all chars after "?" you can use a group to match any char, and you'd better use the "$" sign to indicate the end of line.

Upvotes: 19

Austin Lin
Austin Lin

Reputation: 2564

Check out this site: http://rubular.com/ Basically the site allows you to enter some example text (what you would be looking for on your site) and then as you build the regular expression it will highlight what is being matched in real time.

Upvotes: 2

thejh
thejh

Reputation: 45568

\?(.*)

You want the content of the first capture group.

Upvotes: 367

Related Questions