C # Regex: pick everything up

I want to write everything down to (not including) the # sign in a string. The # character may or may not be present (if it is absent, the entire line must be written).

What will be the RegEx and C # code for this? I tried: ([^ #] +) (?: #), But it does not seem to work.

+4
source share
3 answers

Try:

. * (? = #) Strike>

I think this should work

EDIT:

^[^#]* 

In code:

 string match = Regex.Match(input,"^[^#]*").Value; 
+2
source

Not a regular expression, but an alternative to trying. You can use a regular expression, but for this particular situation, I prefer this method.

 string mystring = "DFASDFASFASFASFAF#322323"; int length = (mystring.IndexOf('#') == -1) ? mystring.Length : mystring.IndexOf('#'); string new_mystring = mystring.Substring(0, length); 
+2
source

What is wrong with something simple:

 [^#]* 

Just take the first match?

+1
source

Source: https://habr.com/ru/post/1306048/


All Articles