The string can contains ints, floats and hexadecimal numbers for example.
“This a string than can have -345 and 57 and could also have 35.4656 or a subtle 0xF46434 and more”
What could I use to find these numbers in C#?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use something along these lines: (I wrote it myself, so I’m not going to say it’s all-inclusive for whatever sort of numbers you’re looking to find, but it works for your example)
Regex seems to be a write-only language, (i.e. incredibly hard to read) so I’ll break it down so you can understand:
(?<=(^|[^a-zA-Z0-9_^]))is a lookbehind to break it by a word boundary. I can’t use\bbecause it considers-a boundary character, so it would only match345instead of-345.-?\d+(\.\d+)?matches decimal numbers, optionally negative, optionally with fractional digits.-?0[xX][0-9A-Fa-f]+matches hexadecimal numbers, case insensitive, optionally negative. Finally,(?=([^a-zA-Z0-9_]|$))is a lookahead, again as a word boundary. Note that in the first boundary, I allowed for the start of the string, and here I allow for the end of the string.