Extract multiple string matches with a regular expression - objective-c

I'm trying to extract multiple parts of a string based on a regular expression like (mymatch1|mymatch2|mymatch3)
I found a function like below, that helps me to extract a match.
NSRange range = [myString rangeOfString:myRegex options:NSRegularExpressionSearch];
But there is any function that can return a set of ranges? or directly the array of substrings matched?
Thanks!

Have you looked at the documentation for NSRegularExpression, methods -[NSRegularExpression enumerateMatchesInString:options:range:usingBlock:]

Related

Regex for extracting certain information from a string

Below is the string that I have -
vdp_plus_forecast_aucc_VDP_20221024_variance_analysis_20221107_backcasting_actuals_asp_True_vlt_True.csv
I need RegEx to take out following items from the string -
20221107
vlt_True
Need help with writing right RegEx for these two extractions. I'm performing the operation on a PySpark DF.
I'm assuming that the answer is based on the variable in front of it so it's capturing the value of variance analysis:
(?<=_variance_analysis_)[0-9]+|vlt_(True|False)
This should capture the variables you wanted, if you only need the value of vlt, you can replace vlt_ with (?<=_vlt) which will just capture the value without the variable

Regex: possible to match repeated patterns in openrefine?

In openrefine I'm trying, for example, to get all the occurences of [aeio]+ in "abeadsabmoloei", in an array : ["a","ea","a","o","oei"]
Let's suppose we don't know the content of the string.
Is it possible with match function?
The match() function is not made to find multiple instances of a pattern in the same string. This is why a discussion is under way to implement a find() or findAll() function. In the meantime, two lines of Python/Jython will do the trick:
import re
return re.findall(r"[aeio]+", value)

Match multiple occurences of string A in string B in business objects

Is there a way to match multiple occurences of string A in string B? Match only return a boolean and Pos only returns the index of the first match it finds.
Maybe split the string into an iterable array based on a common delimiter and return a count somehow?
Well this is one way to solve it. It's not pretty but it works.
=(Length([stringB]) - Length(Replace([stringB];"stringA";""))) / Length("stringA")

Java - Index a String (Substring)

I have this string:
201057&channelTitle=null_JS
I want to be able to cut out the '201057' and make it a new variable. But I don't always know how long the digits will be, so can I somehow use the '&' as a reference?\
myDigits substring(0, position of &)?
Thanks
Sure, you can split the string along the &.
String s = "201057&channelTitle=null_JS";
String[] parts = s.split("&");
String newVar = parts[0];
The expected result here is
parts[0] = "201057";
parts[1] = "channelTitle=null_JS";
In production code you chould check of course the length of the parts array, in case no "&" was present.
Several programming languages also support the useful inverse operation
String s2 = parts.join("&"); // should have same value like s
Alas this one is not part of the Java standard libs, but e.g. Apache Commons Lang features it.
Always read the API first. There is an indexOf method in String that will return you the first index of the character/String you gave it.
You can use myDigits.substring(0, myDigits.indexOf('&');
However, if you want to get all of the arguments in the query separately, then you should use mvw's answer.

VB.Net Regular expression

I want to have a string in the following format
"FAG001 FAG002 FAG003"
and want to split it into
"FAG001"
"FAG002"
"FAG003"
using a regular expression. Unfortunately my knowledge of regular expression synatax is limited to say teh least. I have tried things like
Dim result = Regex.Split(npcCodes, "([A-Z]3[0-9]3)").ToList
without luck
No need of regex here, you could use String.Split
Dim result As String() = npcCodes.Split(new Char[]{" "})
But if you really want to use regex :
Dim result = Regex.Split(npcCodes, " ").ToList()
As madgnome has pointed out you don't need regular expressions here if the string is always separated with spaces.
However for your information the error you made was that you need curly braces for numeric quantifiers:
[A-Z]{3}
And instead of Regex.Split you can uses Regex.Matches.
The regular expression to use in the Split method would be really simple:
Dim result = Regex.Split(npcCodes, " ").ToList
As the expression only matches a single character, you can just as well use the regular Split method in the String class:
Dim result = npcCodes.Split(" "C).ToList