Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple regex for matching up to an optional character?

Tags:

regex

c#-4.0

I'm sure this is a simple question for someone at ease with regular expressions:

I need to match everything up until the character #

I don't want the string following the # character, just the stuff before it, and the character itself should not be matched. This is the most important part, and what I'm mainly asking. As a second question, I would also like to know how to match the rest, after the # character. But not in the same expression, because I will need that in another context.

Here's an example string:

topics/install.xml#id_install

I want only topics/install.xml. And for the second question (separate expression) I want id_install

like image 677
Anders Avatar asked Aug 14 '11 21:08

Anders


1 Answers

First expression:

^([^#]*)

Second expression:

#(.*)$
like image 140
Shef Avatar answered Oct 20 '22 00:10

Shef