regex string length validaiton [duplicate] - regex

This question already has answers here:
How to find the length of a string in R
(6 answers)
Closed 8 years ago.
In R, I need to remove string that exceeds the length of 7 characters, from a column in a data frame.
My code is,
memos.to <- as.data.frame(apply(memos.to,2,function(x)gsub('/^[a-zA-Z0-9]{7,}$/', NA ,x)))
and it doesn't seem to work. What's wrong here?

The easiest way is to just check the string length.
Don't know R lang, but all things being equal, if it conforms to the minimal modern regex's
One of these should match as far as regex is concerned
/.{8,}/ using Dot-all modifier as external flag
or
/(?s).{8,}/
or
/[\S\s]{8,}/ if Dot-all not available
if you are only considering [a-zA-Z0-9] chars
/^[a-zA-Z0-9]{8,}$/

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...

Regex capture groups of N and the remainder? [duplicate]

This question already has answers here:
Split large string in n-size chunks in JavaScript
(23 answers)
Closed 2 years ago.
Supposing, I have a string ASDFZXCVQW, is it possible to capture this into groups of N, and then the remaining characters would be in the final group.
For example, if N were 4, then we could have: ASDF, ZXCV, and QW. Notice how the QW is everything that is left over.
I know how to capture the groups of N with .{N}, and then manually get the leftover through string indexing, but is it possible to do this in a single regular expression?
var data = 'ASDFZXCVQW'
var result = data.match(/\D{1,4}/g)
console.log(result)
It will be helpful!
That really depends on the language in use.
In general, it will be a concatenation of 0 or more 4-character groups followed by 0-3 single characters.
Here is a possible formal definition for alphanumeric string: ([a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9])*[a-zA-Z0-9]*. Different languages might express this differently and possibly in a more compact way.

Regex.Matches problem in visual basic 2010 [duplicate]

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.

How to match all strings that has only one dot using regular expression [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I need to capture strings containing only one dot. String will mostly contains domain names like
test.com, fun.test.com, lesh.test.com.
I need to check only the first one and to ignore the string that has more than one dots.
How can I do this using regex?
Like this :
^[^.]+\.[^.]+$
Check explanations https://regex101.com/r/mn7Ccr/1

regex to grab int/floats and exclude text [duplicate]

This question already has answers here:
RegEx for both, integer and float [closed]
(3 answers)
Closed 4 years ago.
trying to write the appropriate regex expression to capture barometric pressure with two string possibilities. looking to simply grab the float values and remove the "in" string.
The String possibilities are (examples):
'30.01in'
or
'30in'
my current expression (see below) works for the former (30.01), but fails to grab the float in the latter (30in)
re.compile('[0-9]?[0-9]\...')
(\d+(?:\.\d+)?)in
This should capture ints or floats