How can I extract file extension from string? [duplicate] - regex

This question already has answers here:
How to use regex to get file extension?
(3 answers)
Closed 6 months ago.
We have a custom field defined in data studio which extracts and returns the file extension from a string in this case it is from the event label.
I have been using the below with some success
REGEXP_EXTRACT(Event Label, '\\.([\\w\\.-]+)$')
However I'm finding if the string contains multiple periods its including that aswell
Eg it's also extracting text like
07.21.pdf
7.22.PDF
07.21.docx
docx.pdf
How can I tweak my regex to only include from the last period and ignore any earlier.

You could try replacing [\\w\\.-] with [^\\.]
\\.([^\\.]+)$
[^\\.] will match everything except for ., so the match will not be able to contain dots inside.
The full formula would look like this:
REGEXP_EXTRACT(Event Label, '\\.([^\\.]+)$')

Related

take each line of a list and create one long single lined string wrapped by speechmarks [duplicate]

This question already has answers here:
How to edit all lines in Visual Studio Code
(9 answers)
Closed 3 years ago.
So essentially someone on our dev team made a bit of a big issue where they had changed all of the build actions on .csproj files and we are thinking of the easiest way to change them back.
We want to use regex to open all the csproj files via VSCode. The format to open multiple files in file explorer is
"filename" "filename1" "filename2"
my list is
com.Console.job1.csproj
com.Console.job2.csproj
com.Console.job3.csproj
com.Console.job4.csproj
my current regex
(.+)\n
then my regex to replace is
"$1"\s
which doesnt work at all
You can use this regex :
To locate pattern
([^\s]+)(\n)?
To replace :
"$1"
keep in mind the replace pattern contains a space at its end
Demo :
Here

Regex extract number from a string with a specific pattern in Alteryx [duplicate]

This question already has answers here:
Find numbers after specific text in a string with RegEx
(3 answers)
Closed 3 years ago.
I have string like this which looks like a url
mainpath/path2/abc/PI 6/j
From the string I need to get the number along with PI
Main problem is the position of PI part wont be always the same. Sometimes it could be at the end. Sometimes at the middle.
So how can I get that number extracted using regex?
I'm really stucked with this
It's as simple as using the RegEx Tool. A Regular Expression of /PI (\d+) and the Output Method of "Parse" should do the trick.
If you're using Alteryx... suppose your field name is [s] and you're looking for [f] (in your example the value of [f] is "PI")... then you could have a Formula tool that first finds /PI by first creating a new field [tmp] as:
SubString([s],FindString([s],"/"+[f])+1)
and then creating the field you're after [target]:
SubString([tmp],0,FindString([tmp],"/"))
From there run [target] through a "Text to Columns" tool to split on the space, which will give you "PI" and "6".

[Regex]::Match() behaving differently inside vs outside an If (that also uses [Regex]::Match() ) [duplicate]

This question already has answers here:
Execute "real life" command line from variable in Powershell
(3 answers)
Closed 4 years ago.
Given a uninstallString of "C:\ProgramData\Package Cache\{56e11d69-7cc9-40a5-a4f9-8f6190c4d84d}\VC_redist.x86.exe" /uninstall I can successfully extract the quoted text with ([Regex]::Match($uninstallString, '^\".*\"').Value). however, if I test to see if the string has the required /uninstall bit, then try to extract the quoted bit, like this...
if ([Regex]::Match($uninstallString, '^\".*\" +/uninstall').Succes) {
([Regex]::Match($uninstallString, '^\".*\"').Value)
}
Instead of the value being the full string, it's only returning "C:\ProgramData\Package. Now, My understanding is that . is everything but a line break, so it should be OK with the space. But, if I replace the space with an underscore in the string it works as expected, so it's definitely the space causing the issue.
Also, I am confused why it works outside of the If, but not inside. I was under the impression that using [Regex]::Match() creates individual objects with each use, that wouldn't interact with each other, but here it seems they are.
Since you want to see if the quoted string (path) is found AND if it contains a switch '/uninstall' or not,
I'd do something like this:
$uninstallString = '"C:\ProgramData\Package Cache\{56e11d69-7cc9-40a5-a4f9-8f6190c4d84d}\VC_redist.x86.exe"'
if ($uninstallString -match '^(?<path>".*")(?:\s+(?<switch>/uninstall))?') {
$uninstallPath = $matches['path'] # at least the path (quoted string) is found
$uninstallSwitch = $matches['switch'] # if '/uninstall' switch is not present, this will result in $null
}

How to write Reg Expression to Ignore a file path [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 5 years ago.
I 've a File structure "solution/"
Need a reg exp to match all the files under "solution/" Excluding .img files in the path "solution/tree/changes/sample.img"
I tried below it but of not worked
^solution\/tree\/changes\/((?!.img).)*$
Thanks in advance
You did not mention language, but this should work
^solution\/tree\/changes\/(?!.*\.img).*$
see regex101 demo: https://regex101.com/r/Y7znBy/1
Important! You need to set multilineflag
#Fallenhero My Exact requirement is I 've following file structure
solution/moon/
solution/tree/anomoly/
solution/tree/enquiry/
solution/tree/changes/space.img
solution/tree/changes/mine.txt
I've to parse a json file as input with regular expression to copy all the files in the above structure except solution/tree/changes/space.img
JSON Input which I've tried but didn't work
"^solution\/(?!tree\/).*\/*",
"^solution\/tree\/(?!changes\/).*\/*",
"^solution\/tree\/changes\/((?!.img).)*$"
Could you please provide your ideas

Regex pattern for image (not link) [duplicate]

This question already has answers here:
Using regular expressions to parse HTML: why not?
(18 answers)
Closed 8 years ago.
Can anyone give me the pattern of the regex to get all the local images ONLY. :(
I'm using the code below but it includes the image links.
Regex rgx = new Regex("[^\\/:*?\"<>|]+\\.(?i)(jpg|png|gif|bmp)", RegexOptions.IgnoreCase);
input:
http://dl9.glitter-graphics.net/pub/846/846279rr8zhg26y6.gif
images/strawberries.jpg
output:
846279rr8zhg26y6.gif
strawberries.jpg
I don't want the 846279rr8zhg26y6.gif
If your input strings are bounded somehow (e.g. inside ""s or at the start of the input string) then things are easier, and if the input has a semantic context of being a (relative or absolute) URL, and you only want relative URLs, then easier still.
I would however note that the extension part should probably be jpe?g rather than just jpg and I do wish people would never ever use a bmp on a webpage!