How to check one string contains another sub string in dart/flutter - regex

I want to check weather a String contains or not a specific substring in dart/flutter.
example
String mainString = "toyota allion 260 2010";
String substring = "allion";
I want to check weather substring is in the mainString or not?
Please help me to find a solution

String mainString = "toyota allion 260 2010";
String substring = "allion";
mainString.contains(substring); //return true if contains

Related

Split String into 2 sub strings on first occurrence of comma?

I am trying to split String in to 2 sub_strings on the first appearance of comma character.
Also the first occurred comma must be removed.
Example :
Suppose this is the string
Manhattan Ave, Brooklyn,NY,USA
dividing this into this
Manhattan Ave
Brooklyn,NY,USA
also to note that first comma has been removed.
and finally saving these sub_string into variables.
String Place = "Manhattan Ave, Brooklyn,NY,USA";
String result_1 = Place.substring(0, Place.indexOf('.'));
String result_2 ="";
You can try this,
String place = "Manhattan Ave, Brooklyn,NY,USA";
int index = place.indexOf(',');
String result1 = place.substring(0,index).trim();
String result2 = place.substring(index+1).trim();

Regex to find string start with # and end with # example #string demo# in string

Make Regex to find string start with '#' and end with '#' example #string demo# in string.
You can try this:
/\s*(#[^#]*#)\s*/
Demo
Try this regex.
^#.*#$
Example
String s1 = new String("#helloaaaaaa#"); //input string to be tested
boolean result = s1.matches("^#.*#$"); //if input matches i.e (true or false)

Verify and cut a string using regexp in matlab

I have the following string:
{'output',{'variable','VGRG_Pos_Var1/Parameters/D_foo'},'date',734704.60904050921}
I would like to verify the format of the string that the word 'variable' is the second word and i would like to retrive the string after the last '/' in the 3rd string (In this example 'D_foo').
how could i verify this and retrive the sting i search?
I tried the following:
regexp(str,'{''\w+'',{''variable'',''([(a-z)|(A-Z)|/|_])+')
without success
REMARK
The string to analysis is not splited after the komma, it is only due to length of the string.
EDIT
my string is:
'{''output'',{''variable'',''VGRG_Pos_Var1/Parameters/D_foo''},''date'',734704.60904050921}';
and not a cell, which could be understood. I added the sybol ' at the start and end of the string to symbolizied that it is a string.
I realise that you mention using regexp in the question, but I'm not sure if this is a requirement? If other solutions are acceptable you could try this:
str='{''output'',{''variable'',''VGRG_Pos_Var1/Parameters/D_foo''},''date'',734704.60904050921}';
parts1=textscan( str, '%s','delimiter',{',','{','}'},'MultipleDelimsAsOne',1);
parts2=textscan( parts1{1}{3}, '%s','delimiter',{'/',''''},'MultipleDelimsAsOne',1);
string=parts2{1}{end}
match=strcmp(parts1{1}{2},'variable')
To answer the first part of your question, you can write this:
str = {'output',{'variable','VGRG_Pos_Var1/Parameters/D_foo'},'date',734704.60904050921};
temp = str(2); %this holds the cell containing the two strings
if cmpstr(temp{1}(1), 'variable')
%do stuff
end
For the second part you can do this:
str = {'output',{'variable','VGRG_Pos_Var1/Parameters/D_foo'},'date',734704.60904050921};
temp = str(2); %like before, this contains the cell
temp = temp{1}(2); %this picks out the second string in the cell
temp = char(temp); %turns the item from a cell to a string
res = strsplit(temp, '/'); %splits the string where '/' are found, res is an array of strings
string = res(3); %assuming there will always be just 2 '/'s.

Removing data from string using regular expressions in C Sharp

Definitely I'm not good using regular expressions but are really cool!, Now I want to be able to get only the name "table" in this string:
[schema].[table]
I want to remove the schema name, the square brackets and the dot.
so I will get only the work table
I tried this:
string output = Regex.Replace(reader["Name"].ToString(), #"[\[\.\]]", "");
So you came up with a new idea?? Here is what you can try:
string input = "[schema].[table]";
// replacing the first thing into [] with the dot with empty
string one = Regex.Replace(input, #"^\[.*?\]\.", "");
// or replacing anything before the dot with empty
// string two = Regex.Replace(input, #".*[.]", "");
try this
string strRegex = #"^\[.*?\]\.";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = #"[schema].[table]";
string strReplace = #"";
var result=myRegex.Replace(strTargetString, strReplace);
Console.WriteLine(result);
Why do you want to do replace if you just want to extract part of string?
string table = Regex.Match("[schema].[table]", #"\w+(?=]$)").Value;
It works even in case if you don't have schema.

find value after nth occurence of - using RegEx

This expression
[A-Z]+(?=-\d+$)
find SS and BCP from following string
ANG-B31-OPS-PMR-MACE-SS-0229
ANG-RGN-SOR-BCP-0004
What I want to do is find the value after third "-" which is
PMR in first string and BCP in second string
Any help will be highly appreciated
The lookbehind and lookahead will exclude the pre and post part from the match
string mus = "ANG-B31-OPS-PMR-MACE-SS-0229";
string pat = #"(?<=([^-]*-){3}).+?(?=-)";
MatchCollection mc = Regex.Matches(mus, pat, RegexOptions.Singleline);
foreach (Match m in mc)
{
Console.WriteLine(m.Value);
}
What about simple String.Split?
string input = "ANG-B31-OPS-PMR-MACE-SS-0229";
string value = input.Split('-')[3]; // PMR
If you have the option it would be simpler to locate the third "-" and take a substring of the input. See nth-index-of.
var input = "ANG-B31-OPS-PMR-MACE-SS-0229";
input = input.Substring(input.NthIndexOf("-", 3) + 1, 3);