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

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

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.

How to filter out c-type comments with regex? [duplicate]

This question already has answers here:
Regex to match a C-style multiline comment
(8 answers)
Improving/Fixing a Regex for C style block comments
(5 answers)
Strip out C Style Multi-line Comments
(4 answers)
Closed 3 years ago.
I'm trying to filter out "c-style" comments in a line so i'm only left with the words (or actual code).
This is what i have so far: demo
regex:
\/\*[^\/]*[^\*]*\*\/
text:
/* 1111 */ one /*2222*/two /*3333 */ three/* 4444*/ four /*/**/ five /**/
My guess is that this expression might likely work,
\/\*(\/\*\*\/)?\s*([^\/*]+?)\s*(?:\/?\*?\*?\/|\*)
or we would modify our left and right boundaries, if we would have had different inputs.
In this demo, the expression is explained, if you might be interested.
We can try doing a regex replacement on the following pattern:
/\*.*?\*/
This matches any old-school C style comment. It works by using a lazy dot .*? to match only content within a single comment, before the end of that comment. We can then replace with empty string, to effectively remove these comments from the input.
Code:
Dim input As String = "/* 1111 */ one /*2222*/two /*3333 */ three/* 4444*/ four /*/**/ five /**/"
Dim output As String = Regex.Replace(input, "/\*.*?\*/", "")
Console.WriteLine(input)
Console.WriteLine(output)
This prints:
one two three four five

Regexp for string stating with a + and having numbers only [duplicate]

This question already has answers here:
Match exact string
(3 answers)
Closed 4 years ago.
I have the following regex for a string which starts by a + and having numbers only:
PatternArticleNumber = $"^(\\+)[0-9]*";
However this allows strings like :
+454545454+4545454
This should not be allowed. Only the 1st character should be a +, others numbers only.
Any idea what may be wrong with my regex?
You can probably workaround this problem by just adding an ending anchor to your regex, i.e. use this:
PatternArticleNumber = $"^(\\+)[0-9]*$";
Demo
The problem with your current pattern is that the ending is open. So, the string +454545454+4545454 might appear to be a match. In fact, that entire string is not a match, but the engine might match the first portion, before the second +, and report a match.

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\] = )([^,]*)(?=,)

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