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

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

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 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 find sting in the middle of two strings [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 5 years ago.
I want to get the time in the following line. I want to get the string
2017-07-07 08:30:00.065156
in
[ID] = 0,[Time] = 2017-07-07 08:30:00.065156,[access]
I tried this
(?<=[Time] = )(.*?)(?=,)
Where i want to get the string in-between the time tag and the first comma but this doesn't work.
[Time] inside a regex means a T, an i, an m, or an e, unless you escape your square brackets.
You can drop the reluctant quantifier if you use [^,]* in place of .*:
(?<=\[Time\] = )([^,]*)(?=,)

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