Regex.Matches problem in visual basic 2010 [duplicate] - regex

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I have this string
copiaElementos = "c'8 d'8 a8"
And when I do Regex.Matches(copiaElementos, "8.").Count() it returns 2
why is that? I don't understand, can anyone please give me a hand?
Thank you, best regards

That is because the . mathes one character. means you are matching an 8 followed by any charactrer, and there are exactly two of those (a space is considered a character too). Because the last one has no characters after it.
if you want to count the 8s in the string you should do Regex.Matches(copiaElementos, "8").Count(). Remember every character, even a space has its own meaning in regex.

Related

RE2 Match from first character AFTER a character until FIRST space [duplicate]

This question already has answers here:
Regular expression to stop at first match
(9 answers)
How do I match everything after # until space?
(4 answers)
Regex everything after x until space
(1 answer)
Closed 28 days ago.
I really tried hard looking over the internet for an hour or so, trying to find if this question has already an answer somewhere, but no joy. If it already has an answer somewhere, feel free to link and close this one.
I am trying to match from AFTER a specific character until the FIRST space.
This is an example of the source string
blabla/1.2.3 [other stuff I dont care about]
I just want 1.2.3
I have tried so many different variants which I am not gonna pollute here all of them.
But the one I am most intrigued about is
\/.*\s
Apart from matching the / which I want to exclude, why does this match until the end of the line and not until the first space?
Other things I have tried
\/\b This just matches /
\/.*\b Matches almost everything until ]
\/.*\s? Again until end of line
\/.*(\s)? Ditto
\/.*\ Matches until the LAST whitespace excluding newline
And so on...

How do I simplify that regex? [duplicate]

This question already has answers here:
Why doesn't [01-12] range work as expected?
(7 answers)
Closed 2 years ago.
I'm trying to get a remaining time data from a text but the times are written as months, weeks, days or hours rather then number. I've written this regex but it's a bit complicated. How can I simplify it?
[0-99] month[s]?|[0-99] week[s]?|[0-99] day[s]?|[0-99] hour[s]?
Example output:
2 days 4 hours
[0-99] is equivalent to a character set from 0 to 9, plus the character 9 - so it's equivalent to [0-9] - which is (often) equivalent to \d.
A character set with a single character in it is superfluous - just use the single character.
Finally, since the only thing that changes between the alternations is the word, put a group around the word and alternate inside the group:
\d (?:month|week|day|hour)s?\d
That's equivalent to your original pattern. But it sounds like you might be wanting to match up to 2 digits instead, in which case you can tweak it to:
\d{1,2} (?:month|week|day|hour)s?\d{1,2}

JAVA Regex Explain [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
[01]?\d{1,2}
can someone please explain the complete meaning of the above line.
This pattern describes a text that might (but not must) start with a 0 or 1 ([01]?) followed by one to two digits (\d{1,2}).
In short, this pattern used to test a number between 000 and 199. [01]? means a number starts with 0 or 1, and \d{1,2} means a one-digit or two-digit number.

Regex how prevent string containing only the "?" character [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 2 years ago.
I'm searching for a Regex to avoid letting user to enter a string with only a single ? character.
Here are examples of what I want to achieve:
? Not Ok (I want to avoid letting this)
?a Ok
a? Ok
?? Ok
??? Ok
This matches either any one- character string which is different from ? or any string with at least two characters
^([^?]|..+)$

What does + mean for [^SPEAK]+ in RegEx? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I'm finding that learning RegEx is nearly impossible, since every tool or helper site offers the worst explanations and doesn't cover everything. I'm attempting to work through the puzzles on regexcrossword.com and they don't offer substantial help and/or insight. This is causing confusion.
Here is what I understand:
[^SPEAK] = not S, P, E, A, K
+ = matches one or more of the previous characters
Therefore [^SPEAK]+ = nothing
I don't understand how this is supposed to work. What am I missing?
[^SPEAK] matches any character that isn't one of those 5 letters. + means to match the preceding pattern at least 1 time. So [^SPEAK]+ matches a sequence of characters that isn't in that set.
For example, if the input is 123ABCDEFG, it will match 123, BCD and FG.
[^SPEAK] means apart from S,P,E,A,K take any character and + means one or more time
So It will accept any character taken one or more time which s not in the list [SPEAK]
ssssssss is valid string aaaa is valid string SA is invalid string