in reply to Re: Capturing everything after an optional character in a regex?
in thread Capturing everything after an optional character in a regex?

It seems further clarification would be helpful. What do you want to do in the case that there is more than one 'X'? (Or is that case not in your requirements?) If such a case won't exist, or if you want to get everything after the last 'X', I stand by my original suggestion. Use /([^X]*)$/.

If you can have more than one 'X' and you want everything after the first X then something like /^(?:.*?X)?(.*)$/ should do the trick.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Re: Capturing everything after an optional character in a regex?
by Anonymous Monk on Dec 04, 2003 at 14:03 UTC
    There should be only one X. Also, I tried to modify your suggestion to /([^X\s]*)/ since, as in my original code, I only want to grab non-whitespace. But then if the string is 'abcX12  3' I only match abc, when I want to match the '12'. Also, as I said before this is part of a larger regex so I can't use begin/end of line characters.
      how about /.*?X(\S*)|([^X\s]*)/ ?
      since, as in my original code, I only want to grab non-whitespace

      Your original question was, "How can I capture everything after an optional character?" Your code didn't work. Were we supposed to look at your broken code and inuit which parts of it should be considered a specification of your requirements?

      Also, as I said before this is part of a larger regex so I can't use begin/end of line characters.

      You did not say that. You said, "This is a smaller part of a larger regex and I'm looking for a regex solution." There isn't a word there about not being able to match the end of the string. Furthermore, in your "clarification", you went on to say, "If X is not there I want the whole string." Were we supposed to interpret that to mean "the whole string up until some other portion matched by another part of a larger expression?"

      There are many here that would be happy to help you, but you need to effectively communicate what you want help with. Don't take shortcuts by giving us a minimal example if a solution to it isn't really what you need. Don't assume we will understand your problem; make it perfectly clear. Be careful to say what you mean. Remember, you are intimately familiar with your problem and we aren't. We can't read your mind.

      Maybe you've already gotten an answer you can use. Maybe you've gotten an answer that you think is right but which may fail in some cases you haven't considered. Maybe you haven't gotten an answer at all. In any case, my suggestion to you is to repost your question as another SoPW node, but this time be complete in your request. Tell us the whole problem. The very fact that you thought we could easily give you part of a larger regular expression without knowing how it would fit into that larger expression reveals that you probably have a fundamental misunderstanding of how regular expressions usually work. Reasonably complex regexen generally don't just fit together like building blocks. Changing one small part may cause drastically different behavior.

      -sauoq
      "My two cents aren't worth a dime.";