Regular expression to get only the first decimal [duplicate] - c#

This question already has answers here:
Regular expression to match characters at beginning of line only
(8 answers)
Closed 7 years ago.
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?

You can use ^ (start anchor)
^\d+
If you know a specific position, (say 5th) you can do the following:
^.{4}(\d+)

Related

How to create Regular expression from give conditions? [duplicate]

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Learning Regular Expressions [closed]
(1 answer)
Closed 2 years ago.
Actually i am just beginer to learn regex but now i have a problem.
Can somebody help me to create regex for the follwing condtions:
Text lenght should be 6
1st character should be in Caps
2nd character should be in lower
3rd character should be numeric
4th character should be in lower again
5th character should be in Caps again
6th character should be in numeric again
But shouldn't contain any special character
You can do it pretty easily.
([A-Z][a-z]\d{1}[a-z][A-Z]\d{1})

Start of string to have one X and not 2 X's [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
I am trying to find a way of checking if a string starts with only one "X" but not 2 X's in a regular expression.
So I think I have tried
Regex.IsMatch(teststr, "^[^X]{1}?^[XX]");
Regex.IsMatch(teststr, "^[^X]{1}?");
Regex.IsMatch(teststr, "^[^X]{1}");
Regex.IsMatch(teststr, "^[^X]?");
The results should be, if string Starts with 1 X only then give me all except the ones that start with only 1 X. A string with 2 X's is allowed
What about using a negative lookahead?:
^X(?!X)
Basically the (?!pattern) part denotes a negative lookahead, which will fail your overall expression matching if the pattern in the lookahead fails.
Try it online

Regex expression to verify that a string only contains letters, numbers, underscores and dashes [duplicate]

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
What do ^ and $ mean in a regular expression?
(2 answers)
Closed 3 years ago.
I've already read this here and I still have got some questions. I will be very grateful if you can help me to solve them. I'm trying to compose a RegEx to verify that a string contains only letters, numbers, underscores and hyphens. Firstly, when I tried to do it (without Google-search) I did this #"[A-Za-z0-9_-]". After I made some research I found that it should be #"^[a-zA-Z0-9_-]$" where:
^ asserts position at start of a line
$ asserts position at the end of a line
My question is why we should insert these symbols? And my other question is why the string "jeffbutt" (with yellow in the screenshot) doesn't match?

How to make a "minimal match" Regex search in C#? [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 3 years ago.
Let's say I have a multi-line string like this:
STARTFRUIT
banana
ENDFRUIT
STARTFRUIT
avocado
ENDFRUIT
STARTVEGGIE
rhubarb
ENDVEGGIE
STARTFRUIT
lime
ENDFRUIT
I want to search for all fruit, no veggies. I try this:
MatchCollection myMatches = Regex.Matches(tbBlob.Text, "STARTFRUIT.*ENDFRUIT", RegexOptions.Singleline);
foreach (var myMatch in myMatches)
{
Forms.MessageBox.Show(String.Format("Match: {0}", myMatch), "Match", Forms.MessageBoxButtons.OK, Forms.MessageBoxIcon.Information);
}
The problem is, instead of returning me an array of three matches, it gives me a big match encompassing the first STARTFRUIT at the beginning and the last ENDFRUIT at the end. Is there a way to "minimalize" the match search? I don't see any help in RegexOptions.
Use a non-greedy modifier (a question mark) after the quantifier:
"STARTFRUIT.*?ENDFRUIT"
^
add this
Note that the question-mark here has a different meaning here than when it is used as a quantifier, where it means "match zero or one".

How to display a customized code in C# & DevExexpress TextEditor with mask [duplicate]

This question already has answers here:
Is it possible to pad integers with zeros using regular expressions?
(2 answers)
Closed 9 years ago.
I am trying to display a code like ABC/DEF/00012 or ABC/EDF/01234 or ABC/DEF/00009
I use RegEx mask \w{3}/\w{3}/?????
The question mark is hard part that I could not figure it out.
Basically, I try to display the code with characters and numbers. I want to automatically add leading zeros on the number.
Byron
Seems that you're trying to match 5 digits at the end:
\w{3}/\w{3}/\d{5}

Categories