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

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.

Related

Regex match exact string pattern not working [duplicate]

This question already has answers here:
Regular expression pipe confusion
(5 answers)
Closed 20 days ago.
I am sure this question has been answered before, but I cannot find exactly what I was looking for on the Stackoverflow. Would you be kind enough to help me with my issue?
What is the issue? My regex pattern would stop on string #2 the minute is found the same pattern as string #1. In other words, it does not know that "deposit.accountNumberXXX" is not the same as "deposit.accountNumber".
How do I create a pattern that it will be able to make a distinction between string #1 and string #2?
I have two strings.
deposit.accountNumber
deposit.accountNumberXXXX
const findReplace = (valuesToBeReplaced: string, dictionaryKeyValue: { [x: string]: string }) => {
const keyValueString = Object.keys(dictionaryKeyValue).join('|');
const pattern = `${keyValueString}\\b`
const result = new RegExp(pattern, 'g');
// iterate through the Keys of the dictionary and return a replacement value
return valuesToBeReplaced.replace(result, (matched) => dictionaryKeyValue[matched]);
};
I have tried different patterns and could not get it to work.
You need parentheses around ${keyValueString}. Otherwise, the regexp looks like
word1|word2|word3\b
and the
\b word boundary is only applied to the last word.
const pattern = `(?:${keyValueString})\\b`

Regex for substring match [duplicate]

This question already has answers here:
Regex for string contains?
(5 answers)
Closed 7 months ago.
I have list of strings
03000_textbox (57447),
03990_textbox (57499),
03000_textnewbox (57447)
I want to use regex to capture 1st and 2nd elements of list.
Anything that contains substring textbox i.e.
03000_textbox (57447)
03990_textbox (57499)
Here is the regex you're looking for :
[0-9]+_textbox \([0-9]+\)
Live sample : https://regex101.com/r/2oiwcF/1
Don't forget to put a global (g) flag so you can get every match and loop into.

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)$/

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']

How to capture repeating groups in Regex (for C#) [duplicate]

This question already has answers here:
Get string between two strings in a string
(27 answers)
Closed 6 years ago.
I've to detect and extract from a string a repeating group of characters and list one part of each captured group.
Here is an example of string to parse: "za e eStartGood1Endds qStartGood2Endsds df"
My Regex is: ".*?(?::Start(.+)End.*?)+"
Expecting groups captured expected: Good1, Good2, etc
My Regex capture is wrong: it seems that (?::Start(.+) is considered as group to capture...
May I miss something?
Thanks!
This regex do the job :
/(?<=Start)(.+?)(?=End)/g
Why not use this pattern: \*{2}Start\*{2}(.*?)\*{2}End\*{2}
For this input string: za e e**Start**Good1**End**ds dq**Start**Good2**End**sds df, it captures Good1 and Good2.
You can play with it here: https://regex101.com/r/dG0dX6/2