I have a String like - \"Bangalore,India=Karnataka\"
. From this String I would like to extract only the substring \"Bangalore\"
. In this case the regex
Are you somehow forced to solve this using one regexp and nothing else? (Stupid interview question? Extremely inflexible external API?) In general, don't try to make regexes do what plain old programming constructs do better. Just use the obvious regex, and it it doesn't match, return the entire string instead.