Can find "15" in "155", but not "55" [duplicate] - regex-greedy

This question already has answers here:
How can I match overlapping strings with regex?
(6 answers)
Closed 1 year ago.
The expression /\d5/g , matches with "15" in "155", but not "55".
How do I modify the expression so it matches with "15" and "55" in "155"?
Apparently, I have to do something called a "lookahead assertion".
let str = '155';
let regEx = /(?=(\d5))/g;
let matches = str.matchAll(regEx);

I don't know your parser, but maybe you have to say, that the "5" can occure more-times.
Something like this:
let regEx = /(?=(\d5)*)/g;

Related

How to find words with a specific letter using RegExp? [duplicate]

This question already has answers here:
How to find words containing a certain letter with Regular Expressions?
(2 answers)
Closed 1 year ago.
void main() {
var string = 'This is a statement with i.';
var regExp = RegExp('i');
for (var match in regExp.allMatches(string)) {
print(match.group(0));
}
}
I want to print all the words which has i in it by only using RegExp. For instance, the above for loop should print
This
is
with
i
Would checking for a word boundary work for you?
The pattern \b\w*i\w*\b with the global flag set matches:
This
is
with
i
Try it out!
I don't have access to my GitHub (to share a gist) at the moment, but this pattern works as expected in DartPad.

Regex negative match at end of string [duplicate]

This question already has an answer here:
Find 'word' not followed by a certain character
(1 answer)
Closed 2 years ago.
I need an regex that match
"my dog", "My dog", "my &dog"
but not
"my dog#", "My dog#"
for search string "my dog". I have this expression at the moment by I have this:
reg_replace("/\b(my dog)\b/ui",'found','My dog');
But this obviously matches "my dog#" and not "My &dog". Any help would be appreciated.
my..?dog$
. = any Space
.? = 0 or 1 of any Space
$ = end of string
As not casesensitive this doesnt matter.
If you only have these, you can make a ".?" to get this one Dog with "&"
with end of string, you can simply get these other dogs you dont want out there.
Tested with regex101, if only these values are there, this is enough.
Flags = gsmi (Global, Multiline, singleline, insensitive)

Regex express begin & end specific words[No duplicate] [duplicate]

This question already has answers here:
Regex matching beginning AND end strings
(6 answers)
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I'm trying to write a regex represent 'recognizes words that begin and end in "t".'
I think that the below code is true.
var re = /^t+t*t$/
But it shows 'false'
e.g.
re.test('triplet')
re.test('thought')
re.test('that')
why doesn't my answer solve the above string?
and what is the proper regex?
Your regex is wrong, as pointed out in the comments.
A naive approach could be to check if the entire word starts with t, has any number of any character and then ends with t:
var re = /^t.*t$/
of course, you could also limit the "middle" character to letters:
var re = /^t[a-z]*t$/
However, neither of these approaches check for a word that is a single "t" character. If this is a valid usecase, you'll have to handle it explicitly, e.g.:
var re = /^(t[a-z]*t|t)$/

How can I remove a certain pattern from a string? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I have this string like "682_2, 682_3, 682_4". (682 is a random number)
How can i get this string "2, 3, 4" using regex and ruby?
You can do this in ruby
input="682_2, 682_3, 682_4"
output = input.gsub(/\d+_/,"")
puts output
A simple regex could be
/_([0-9]+)$/ and in the match group of the result you will have 2 for 682_2 and 3 for 682_3
Ruby code snippet would be "64532_2".match(/_([0-9]+)/).captures[0]
you can use scan which returns an array containing the matches:
string_code.scan(/(?<=_)\d/)
(?<=_) tells to find a pattern that has a given pattern (_ in this case) before itself but wont capture that, it captures only \d. if it can have more than 1 digit like 682_13,682_33 then \d+ is necessary.

Regex to match all character groups in a string [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 5 years ago.
I need a regex to match the groups of characters in a string.
For example this is-a#beautiful^day.
Should result in the following list: this, is, a, beautiful, day.
As a mention I don't know how long the string is or by what characters the words are separated.
Any ideas? I have no clue how to build a regex for this.
If you want find all groups of letters:
import re
string = "this is-a#beautiful^day"
list = re.findall(r'[A-Za-z]+', string)
print list
['this', 'is', 'a', 'beautiful', 'day']