194

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.

Mark
  • 2,569
  • 3
  • 17
  • 8

7 Answers7

358
\?(.*)

You want the content of the first capture group.

thejh
  • 44,854
  • 16
  • 96
  • 107
  • 7
    @Dreamonic Don't think it's happening :( – Starkers Sep 28 '13 at 04:54
  • 7
    @Starkers You right. It won't happen. Mark hasn't been on since `Feb 5 '11 at 10:41`. It is still good to point out every change we get. Maybe someone will learn from Dreamonic pointing this out. – DutGRIFF Feb 09 '14 at 08:19
  • 2
    how would you capture the content after the last question mark? i want to 'anchor' from thereafter - im not sure if anchor is the right word. – BenKoshy Nov 29 '15 at 12:48
  • 2
    @BKSpurgeon first capture group in ^.*\?([^?]*)$ should work – thejh Nov 29 '15 at 22:05
  • 2
    Hello from 10 years in the future :). This answer matches everything after the question mark but includes the question mark. How can I remove the question mark from the match? – Matthew Wolman Apr 24 '20 at 19:48
  • What about if I want to capture multiple lines after the question mark? – Carlos Muñiz Sep 29 '21 at 04:29
  • 2
    @Matthew, try `(?<=\?).*` this will only match everything after `?` – user391 Jul 05 '22 at 20:48
107

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:

(?<=\?).*
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • Oh the fixed width? Can I do a look behind for something like this? Topics: code, programming, design So I want to select the colon and look behind as far as the cpaital T of topics and fowards to the end on the line? (in this case end of the line is "design". – Mark Dec 11 '10 at 21:36
  • @Mark: What language are you using? – Mark Byers Dec 11 '10 at 21:38
  • I am using Yahoo Pipes which excepts regex. I got this question answered and now looking for a regex that select 6 chars behind a colon and as many (all) forwards.. – Mark Dec 11 '10 at 21:39
  • @Mark: I checked your list of questions but I don't see your new one. Are you sure you have posted it? – Mark Byers Dec 11 '10 at 21:44
  • I will ask this new question again in a few moments. Guess it's best to keep my questions separate and really should have asked both at the same time. Thanks – Mark Dec 11 '10 at 21:49
59

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.

DarkNeuron
  • 7,981
  • 2
  • 43
  • 48
20
\?(.*)$

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.

s3v3n
  • 8,203
  • 5
  • 42
  • 56
  • 2
    In most regular expression implementations the `.` by default doesn't match the new line character. As a result, even without the end of line character in the expression it would match up to the end of the line. – Mark Byers Dec 11 '10 at 21:30
9

?(.*\n)+

With this you can get everything Even a new line

Hossein Mohammadi
  • 1,388
  • 11
  • 16
2

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.

Austin Lin
  • 2,545
  • 17
  • 17
0

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 .+.

Kadhem
  • 11
  • 5