-2

I have a string of type "12.43 some non important stuff", I want to get the number 12.43, but only if it is at exact position, which is 0 in this case.

"\d+" will return 12.43, but it will return it even in case of "hello world 12.43 some non important stuff".

Is there an easy way (maybe not regex even) to get the decimal only if it's on desired position?

Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207
  • @CodeCaster it is not quite a duplicate of that question, since I need a decimal. So I did not guess to search for just a character and created this one. – Ilya Chernomordik Feb 23 '16 at 13:55
  • It doesn't matter what you're looking for, the only relevant part of your question is that what you're looking for is at the start of the line, which the duplicates shows how to do. I also highly doubt that `d+` will match `12.43`. – CodeCaster Feb 23 '16 at 13:57
  • Well, you are right, it does not :( So apparently it's not only the start of the line that is relevant, but I guess It's yet another question – Ilya Chernomordik Feb 23 '16 at 14:02
  • Well if you're having trouble with that, try searching something like "C# regex match literal dot". – CodeCaster Feb 23 '16 at 14:54
  • I found the solution actually, in the comments to the answer. You can close it of course, but it does not look like full duplicate to me. – Ilya Chernomordik Feb 23 '16 at 15:19
  • Well we really don't need a separate question for each specific regex pattern, searching for relevant questions and answers already is hard enough as it is. Your actual question was _"How can I match only from the beginning of the line"_, which is answered in the duplicate. Your secondary question, _"How can I match a literal dot"_, is answered elsewhere. – CodeCaster Feb 23 '16 at 15:27

1 Answers1

4

You can use ^ (start anchor)

^\d+

If you know a specific position, (say 5th) you can do the following:

^.{4}(\d+)
karthik manchala
  • 13,492
  • 1
  • 31
  • 55