using Regex get substring between underscore 2 and underscore 3 of string, vb.net - vb.net

I have a string like: Title Name_2021-04-13_A+B+C_Division.txt. I need to extract the A+B+C. The A+B+C may be other letters. I believe that using Regex would be the simplest way to do this. In other words I need to get the substring between underscore 2 and underscore 3 of string. All of my code is written in vb.net. I have tried:
boatClass = Regex.Match(myFile, "(?<=_)(.*)(?=_)").ToString
I know this is not right but I think it is close. What do I need to add or change?

The regex code that will extract a substring between the second and third underscore of a string is:
(?:[^_]+_){2}([^_]+)
However, I chose to use the split function:
myString.Split("_"c)(2)

Related

REGEX_EXTRACT for specific pattern inside brackets

Trying to use REGEX_EXTRACT in SQL to extract certain string patterns inside Brackets.
So I have tried this formula: REGEX_EXTRACT(column, r'\[(.*?)\]'), but problem is that there are multiple Brackets in the same cell, and this formula will only extract the first string pattern in the first bracket.
So, what I'm trying to figure out is how can I extract specific patterns within the Brackets? The pattern I'm looking for looks like this: [xx-XX]
Where x can be any string in the alphabet.
Any tips or directions would be greatly appreciated
This should work if you always have 2 lowercase letters followed by '-' and then followed by 2 uppercase letters:
\[([a-z]{2}-[A-Z]{2})\]

sql looking for pattern

I have a string similar to this 'MSH|^~\&|STF_ALL_LAB_IN_C...
I'm trying to find some sql that will bring back all messages that contain
MSH|^~\&|(any 3 characters)_(anything after the underscore).
Tried something like this
WHERE TransText LIKE 'MSH|^~\&|%_%_%_'
But that doesn't seem to require the underscore.
Any suggestions?
MSH|^~&|(any 3 characters)_(anything after the underscore).
The pattern would be:
where TransText like 'MSH|^~\&|___\_%'
In some databases, the backslash would need to be escaped, so that would be:
where TransText like 'MSH|^~\\&|___\_%'
_ is a special character in a LIKE clause. It matches any one character, where % matches any series of 0 or more characters.
You need to escape it, using \_.

Split a string to only use the middle part in SQL

I have a string
ABC - ABCDEFGHIJK - 05/07/2016
I want to only use the ABCDEFGHIJK section and remove the first and third parts of the string.
I have tried using SUBSTRING with CHARINDEX, but was only able to remove the first part of the string.
Anyone help with this?
You can use SUBSTRING_INDEX() and TRIM() for spaces :
SELECT TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(string_col,'-',2),'-',-1)) AS Strig_Col
FROM YourTable;

Using groups in OpenRefine regex

I'm wondering if it is possible to use "groups" in ReGeX used in Open Refine GREL syntax. I mean, I'd like to replace all the dots followed and preceded by a character WITH the same character and dot but followed by a space and then the character.
Something like:
s.replace(/(.{1})\..({1})/,/(1).\s(2)/)
It should, but your last argument needs to be a string, not a regular expression. Internally Refine uses Java's Matcher#replaceAll method which accepts a string argument.
I think I found out how to deal with this. You need to put $X in your string value to address a Xth capture group.
It should be like this:
s.replace(/.?(#capcure group 1).?(#capcure group 2).*?/), " some text $1 some text $2 some text")

How should a string be matched with a regular expression in Objective C

I'm finding it hard to match strings using NSRegularExpression. Generic alpha characters are not a problem with [a-z] but if I need to match a word like 'import' I'm struggling to make it work. I'm sure I have to escape the word in some manner but I can't find any docs around this. A really basic example would be
{{import "hello"}}
where I want to get hold of the string: hello
edit: to clarify - 'hello' could be any string - it's the bit I want returned
This regular expression matches the text between the "-s in your example:
\{\{import "([^"]+)"\}\}
The match will be stored in the first match group.