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.

3
  • 8
    \?.* matches everything (including an empty string) after ?.
    – khachik
    Dec 11, 2010 at 21:22
  • 1
    What programming language are you using?
    – Mark Byers
    Dec 11, 2010 at 21:27
  • 2
    For those using Java, it's "\\?" Jun 23, 2014 at 16:52

7 Answers 7

358
\?(.*)

You want the content of the first capture group.

7
  • 7
    @Dreamonic Don't think it's happening :(
    – Starkers
    Sep 28, 2013 at 4: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 9, 2014 at 8: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, 2015 at 12:48
  • 2
    @BKSpurgeon first capture group in ^.*\?([^?]*)$ should work
    – thejh
    Nov 29, 2015 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? Apr 24, 2020 at 19: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:

(?<=\?).*
5
  • 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, 2010 at 21:36
  • @Mark: What language are you using?
    – Mark Byers
    Dec 11, 2010 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, 2010 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, 2010 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, 2010 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.

2
  • not working on safari Jan 5, 2022 at 13:01
  • this is the solution to the question. Requested was AFTER "?" nice. Thanks! Mar 3, 2022 at 16:15
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.

1
  • 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, 2010 at 21:30
10

?(.*\n)+

With this you can get everything Even a new line

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.

2
  • Thanks for that, do you know how to match a colon? And a certain amount of characters before the colon?
    – Mark
    Dec 11, 2010 at 21:42
  • /([a-zA-Z]{4}):/ will match Test: Welcome and grab Test
    – Austin Lin
    Dec 11, 2010 at 22:30
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 .+.

1
  • Please consider adding a description of your code so we know what it does and why. Mar 8, 2019 at 18:09

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.