Regular expression to pick extension - c++

What would regular expression look like for any string which ends with .txt?
Tried few myself but it doesn't look like I'm getting anywhere.
I'd like to construct a regex object to feed a function.

Something like : .*\.txt$
If you want more precisions, I guess you should precise a language and some other stuffs...

All you have to do is match the end of the string using
/\.txt$/
Matching more than that e.g., .*\.txt$ is not necessary

Assuming Perl-style regular expressions, /[^\.]*\.txt$/ should work.

Related

Matching a substring using a regular expression in PowerShell

Regular expressions are really not my forte and I am trying to learn. Struggling with this one at the moment.
<fraglink id="230681395" resid="1057000484">
I have a file with loads of text in it, and every now and then bits like the above appear in it. I want to get the number in between the quotes after resid=.
Is some sort of look ahead / behind required here?
It looks like you want a regex like:
resid="([0-9]+)"
And to grab $1.
Since your content looks like XML, you should probably not use a regular expression to grab your desired value. If you share your whole file we will show you how to select the value properly using XPath for example.
However, if you want to use a regular expression for training purposes, try this:
$content = Get-Content 'your_file_path' -raw
[regex]::Match($content, '\bresid="([^"]+)').Groups[1].Value
Using a lookahead ((?<=pattern)), your pattern could look like:
(?<=resid=")\d+
With Regex.Match():
$id = [regex]::Match($inputString,'(?<=resid=")\d+').Value

Can someone tell me the regular expression for this?

I am working with regular expressions, I need to create an expression for validating strings against the following scenario:
Solution.<word1|word2|word3>.<word4|word5>.anyword.(any word containing proj in it)
I tried
Solution.\b(word1|word2|word3)\b.\b(word4|word5)\b.(.*).\b(.*proj)\b
But this allows strings like Solution.word1.word4.blabla.blabla.csproj, meaning it allows anything before the proj because of the .*.
Can someone help me with this??
Looks like you need this regex:
Solution\.(word1|word2|word3)\.(word4|word5)\.([^.]+)\..*?\bproj\b
RegEx Demo
You might want to try (need to escape the . and allow capturing group to have chars except .):
Solution\.\b(word1|word2|word3)\b\.\b(word4|word5)\b\.([^\.]*)\.\b([^\.]*proj)\b
It's hard to consider the actual strings you want to allow without more clarification.
You can try the following regular expression.
Solution\.word[123]\.word[45]\.\w+\.\w*proj\b

Regular Expression for recognizing files with following patterns az.4.0.0.119.tgz

I am trying to find a regular expression that will recognize files with the pattern as az.4.0.0.119.tgz. I have tried the regular expression below:
([a-z]+)[.][0-9]{0,3}[.][0-9]{0,3}[.][0-9]{0,3}[.]tgz
But, no luck. Can anyone please point me in the right direction.
Regards,
Shreyas
Better to use a simple regex like this:
^([a-z]+)\.(?:[0-9]+\.)+tgz$
You just forgot one number part:
([a-z]+)[.][0-9]{0,3}[.][0-9]{0,3}[.][0-9]{0,3}[.][0-9]{0,3}[.]tgz
or
([a-z]+)[.]([0-9]{0,3}[.]){4}tgz
Depending on where and how you use the regex, you might want to surround it in ^...$.
Your pattern has 4 chiffers group, your regexp only 3.

Regular Expressions with conditions

I have a string that looks like:
this is a string [[and]] it is [[awesome|amazing]]
I have the following regular expression so far:
(?<mygroup>(?<=\[\[).+?(?=\]\]))
I am basically trying to capture everything inside the brackets. However, I need to add another condition that says: If the matched result contains a pipe delimiter then only return the word to the right of the pipe delimiter. If there is no pipe then just return everything inside the brackets.
The parsing result I am looking for given the example above should look like:
and
amazing
Any input is appreciated.
(?<mygroup>(?<=\[\[)([^|\]]*|)?([^|]+?)(?=\]\]))
You could use this regex:
(?<=\[\[[^\]]*?)(?!\w+\|)\w+(?=\]\])
it matches both and and amazing words in your test example. You could check it out, I created a test app on Ideone.
From the regex info page:
The tremendous power and expressivity
of modern regular expressions can
seduce the gullible — or the foolhardy
— into trying to use regexes on every
string‐related task they come across.
My advice: Just grab what is between the brackets and parse it after.
Regular expressions are not the answer to everything. May those who follow after you be spared from deciphering the regex you come up with.

matching table tag by regular expression in php

I need to match a substring in php substring is like
<table class="tdicerik" id="dgVeriler"
I wrote a regular expression to it like <table\s*\sid=\"dgVeriler\" but it didnot work where is my problem ?
You forgot a dot:
<table\s.*\sid="dgVeriler"
would have worked.
<table\s+.*?\s+id="dgVeriler"
would have been better (making the repetition lazy, matching as little as possible).
<table\s+[^>]*?\s+id="dgVeriler"
would have been better still (making sure that we don't accidentally match outside of the <table>tag).
And not trying to parse HTML with regular expressions, using a parser instead, would probably have been best.
I dont know what you want get but try this:
<table\s*.*id=\"dgVeriler\"