RegularExpression get strings between new lines - regex

I want to taking every string who is located on a new line with Regular Expression
string someStr = "first
second
third
"
example:
string str1 = "first";
string str2 = "second";
string str3 = "third";

Or if you just want the first word of each line;
^(\w+).*$ with multi-line flag.
Regex101 has a nice regex testing tool: https://regex101.com/r/JF3cKR/1

Just split it with "\n";
someStr.split("\n")
And you can filter the empty strings if you'd like
Or if you really want regex, do /^.*$/ with multiline flag
List<String> listOfLines = new ArrayList<String>();
Pattern pattern = Pattern.compile("^.*$", Pattern.MULTILINE);
Matcher matcher = pattern.matcher("first\nsecond\nthird\n");
while (matcher.find()) {
listOfLines.add(matcher.group());
}
Then you have;
listOfLines.get(0) = first
listOfLines.get(1) = second
listOfLines.get(2) = third

You can use the following regex :
(\w+)(?=\n|"|$)
see demo

Related

Trim String/Text in Flutter

Hi I tried to trim a link in flutter
Currently I am looking into regexp but I think that is not possible
This is the link in full:
http://sales.local/api/v1/payments/454/ticket/verify?token=jhvycygvjhbknm.eyJpc3MiOiJodH
What I am trying to do is to trim the link like this:
http://sales.local/api/v1/payments/454
Kindly advise on best practise to trim string/text in flutter. Thanks!
try to use substring() :
String link = 'http://sales.local/api/v1/payments/454/ticket/verify?token=jhvycygvjhbknm.eyJpc3MiOiJodH';
String delimiter = '/ticket';
int lastIndex = link.indexOf(delimiter);
String trimmed = link.substring(0,lastIndex);
//print(trimmed);
input string print for Flutter:
String str2 = "-hello Friend- ";
print(str2.trim());
Output Print : -hello Friend-
NOte: Here last space remove from string.
1.Right Method:
var str1 = 'Dart';
var str2 = str1.trim();
identical(str1, str2);
2.Wrong Method
'\tTest String is Fun\n'.trim(); // 'Test String is Fun'
main(List<String> args) {
String str =
'http://sales.local/api/v2/paymentsss/45444/ticket/verify?token=jhvycygvjhbknm.eyJpc3MiOiJodH';
RegExp exp = new RegExp(r"((http|https)://sales.local/api/v\d+/\w.*?/\d*)");
String matches = exp.stringMatch(str);
print(matches); // http://sales.local/api/v2/paymentsss/45444
}

Regex pattern misses match on a 2 char word

Using regex101 I have developed this regex:
^(\S+)\s_(\S)(\S[^;\s]+)?.*
This works great for 99.999% of the time but occasionally it is run against a string containing a 2 char word that should have matched.
For example it would normally capture...
string _asdf = string.empty;
bool _ttfnow;
//$1 = string
//$2 = a
//$3 = sdf
and
//$1 = bool
//$2 = t
//$3 = tfnow
But for some reason this fails to match the third group?
string _qw = string.empty;
//$1 = string
//$2 = q
//$3 =
Again using regex101 if add add a char it suddenly matches so:
string _qwx = string.empty;
//$1 = string
//$2 = q
//$3 = wx
Any ideas? Thank You
^(\S+)\s_(\S)(\S[^;\s]*)?.*
^^
Just change the quantifier.See demo.
https://regex101.com/r/pG1kU1/33
[^;\s]+ change it to [^;\s]*
/^(\S+)\s_(\S)(\S[^;\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);

Is it possible to use your match from the match box in the replace box with Regex/Replace?

I have a long list like this
sb.AppendLine(product.product_id.Length.ToString())
I want to use regex search and replace to change every line to something like this
sb.AppendLine("product.product_id " + product.product_id.Length.ToString())
Is this possible to do with regex search and replace?
What do I put in the search box and what to I put in the replace box?
Is it possible to use your match from the match box in the replace box?
Yes it is possible, using capturing parentheses.
I assume that product_id is variable.
Search:
sb\.AppendLine\(product\.(.+)\.Length\.ToString\(\)\)
Replace:
sb.AppendLine("product.$1 " + product.$1.Length.ToString())
in vb .net
Dim str As String
str = "sb.AppendLine(product.product_id.Length.ToString())"
Dim RegexObj As Regex
RegexObj = New Regex("^sb.AppendLine((.*).Length.ToString())$")
Dim res As String
res = RegexObj.Replace(str, "sb.AppendLine(""$1"" + product.product_id.Length.ToString())")
in c# .net
string str = null;
str = "sb.AppendLine(product.product_id.Length.ToString())";
Regex RegexObj = default(Regex);
RegexObj = new Regex("^sb\.AppendLine\((.*)\.Length\.ToString\(\)\)$");
string res = null;
res = RegexObj.Replace(str, "sb.AppendLine(\"$1\" + product.product_id.Length.ToString())");