I would like to have a regualr expression for the string where output would be like:
CP_RENOUNCEABLE
CP_RIGHTS_OFFER_TYP
CP_SELLER_FEED_SOURCE
CP_SELLER_ID_BB_GLOBAL
CP_PX
CP_RATIO
CP_RECLASS_TYP
I tried using regex with
string pattern = #"ISNULL(*)";
string strSearch = #"
LTRIM(RTRIM(ISNULL(CP_RENOUNCEABLE,'x2x'))), ISNULL(CP_RIGHTS_OFFER_TYP,-1), LTRIM(RTRIM(ISNULL(CP_SELLER_FEED_SOURCE,'x2x'))),
LTRIM(RTRIM(ISNULL(CP_SELLER_ID_BB_GLOBAL,'x2x'))),ISNULL(CP_PX,-1), ISNULL(CP_RATIO,-1), ISNULL(CP_RECLASS_TYP,-1);
string pattern = #"ISNULL(*\)";
foreach (Match match in Regex.Matches(strSearch, pattern))
{
if (match.Success && match.Groups.Count > 0)
{
var text = match.Groups[1].Value;
}
}
My guess is that we'd be having a comma after our desired outputs listed in the question, which then this simple expression might suffice,
(CP_[A-Z_]+),
Demo 1
If my guess wasn't right, and we would have other chars after that such as an space, we can add a char class on the right side of our capturing group, such as this:
(CP_[A-Z_]+)[,\s]
and we would add any char that might occur after our desired strings in [,\s].
Demo 2
Test
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = #"(CP_[A-Z_]+),";
string input = #"LTRIM(RTRIM(ISNULL(CP_RENOUNCEABLE,'x2x'))), ISNULL(CP_RIGHTS_OFFER_TYP,-1), LTRIM(RTRIM(ISNULL(CP_SELLER_FEED_SOURCE,'x2x'))),
LTRIM(RTRIM(ISNULL(CP_SELLER_ID_BB_GLOBAL,'x2x'))),ISNULL(CP_PX,-1), ISNULL(CP_RATIO,-1), ISNULL(CP_RECLASS_TYP,-1);";
RegexOptions options = RegexOptions.Multiline;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
Edit:
For capturing what is in between ISNULL and the first comma, this might work:
ISNULL\((.+?),
Demo 3
Related
I have many strings like these
/test/v1/3908643GASF/item
/test/v1/343569/item/AAAS45663/document
/test/v2/field/1230FRE/item
...
For each one I need to extract the defined pattern like these
/test/v1/{Value}/item
/test/v1/{Value}/item/{Value}/document
/test/v2/field/{Value}/item
The value can be a guid or something else, Can I match the given string patterns with input paths with regex?
I wrote just this code but I don't konw how to match input paths with patterns. The result should be the pattern. Thank you
string pattern1 = "/test/v1/{Value}/item";
string pattern2 = "/test/v1/{Value}/item/{Value}/document";
string pattern3 = "/test/v2/field/{Value}/item";
List<string> paths = new List<string>();
List<string> matched = new List<string>();
paths.Add("/test/v1/3908643GASF/item");
paths.Add("/test/v1/343569/item/AAAS45663/document");
paths.Add("/test/v1/343569/item/AAAS45664/document");
paths.Add("/test/v1/123444/item/AAAS45688/document");
paths.Add("/test/v2/field/1230FRE/item");
foreach (var path in paths)
{
}
This can also be achieved using regex alone. You can probably try:
(\w+)\/\w+(?<=\/item)(\/(\w+)\/)?
Explanation of the above regex:
(\w+) - Represents a capturing group matching a word character one or more time. This group captures our required result.
\/\w+(?<=\/item) - Represents a positive look-behind matching the characters before \items.
$1 - Captured group 1 contains the required information you're expecting.
(\/(\w+)\/)? - Represents the second and third capturing group capturing if after item some other values is present or not.
You can find the demo of the above regex in here.
Sample implementation in C#:
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = #"(\w+)\/\w+(?<=\/item)(\/(\w+)\/)?";
string input = #"/test/v1/3908643GASF/item
/test/v1/343569/item/AAAS45663/document
/test/v2/field/1230FRE/item";
foreach (Match m in Regex.Matches(input, pattern))
{
Console.Write(m.Groups[1].Value + " ");
if(m.Groups[3].Value != null)
Console.WriteLine(m.Groups[3].Value);
}
}
}
You can find the sample run of the above implementation in here.
I have a text like this
#-cmd1-# Hakona Matata #-cmd2-#
I want to get all values that is like this #-TEXT-#
Here's the code I use, but it works only when there's one occurrence
var text = "#-adsfree-# hakona matata #-adsbottom-#";
Regex regex = new Regex("#-(.*)-#");
var v = regex.Match(text);
string s = v.Groups[1].ToString();
I'm guessing that you might be designing an expression, maybe similar to:
(?<=#-)\b(\w+)\b(?=-#)
Test
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = #"(?<=#-)\b(\w+)\b(?=-#)";
string input = #"#-adsfree-# hakona matata #-adsbottom-#";
RegexOptions options = RegexOptions.Multiline;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
The expression is explained on the top right panel of this demo if you wish to explore/simplify/modify it.
I have a long text which contains strings like these:
...
1.1SMITH/JOHN 2.1SMITH/SARA
...
1.1Parker/Sara/Amanda.CH07/Elizabeth.IN03
...
Is there any regular expression in C# which can match these names. The clue is to search for [A-Z] which has separated by '/'.
You can try this:
[a-zA-Z\/]+
Explanation
c# sample:
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = #"[a-zA-Z\/]+";
string input = #"...
1.1SMITH/JOHN 2.1SMITH/SARA
...
1.1Parker/Sara/Amanda.CH07/Elizabeth.IN03";
foreach (Match m in Regex.Matches(input, pattern))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
You can test the working c# sample here
You can use
[a-z\/]+
which matches any combination of characters and slashes (see Regex101).
Make sure you are matching case-insensitive.
var expression = new Regex(#"[a-z\/]+", RegexOptions.IgnoreCase);
var names = expression.Matches(theText, expression);
Do you want to capture any [A-Za-z] which has a previous char or next char equals '/'?
Try this:
(?<=\/)[A-Za-z]+|[A-Za-z]+(?=\/)
I'm building a regex and I'm missing something as it's not working properly.
my regex logic is trying to look for anything that has #anychars# and return the number of matches on the sentence and not a single match.
Here are a few examples
1- #_Title_# and #_Content_# should return two matches: #_Title_# and #_Content_#.
2- Product #_TemplateName_# #_Full_Product_Name_# more text. text text #_Short_Description_# should return 3 matches: #_TemplateName_# #_Full_Product_Name_# and #_Short_Description_#
and so on. Here is what my regex looks like: ^(.*#_.*_#.*)+$
any thoughts on what I'm doing wrong?
Something as simple as:
#.*?#
Or:
#_.*?_#
If you are trying to match the underscores too (it wasn't clear in the original version of the question). Or:
#_(.*?)_#
Which makes it easier to extract the token between your #_ and _# delimiters as a group.
Should work. The *? is key. It's non-greedy. Otherwise you match everything between the first and last #
So for example:
var str = "Product #_TemplateName_# #_Full_Product_Name_# more text. text text #_Short_Description_#";
var r = new Regex("#_(.*?)_#");
foreach (Match m in r.Matches(str))
{
Console.WriteLine(m.Value + "\t" + m.Groups[1].Value);
}
Outputs:
#_TemplateName_# TemplateName
#_Full_Product_Name_# Full_Product_Name
#_Short_Description_# Short_Description
Try this :
string[] inputs = {
"#Title# and #Content#",
"Product #TemplateName# #_Full_Product_Name_# more text. text text #_Short_Description_#"
};
string pattern = "(?'string'#[^#]+#)";
foreach (string input in inputs)
{
MatchCollection matches = Regex.Matches(input, pattern);
Console.WriteLine(string.Join(",",matches.Cast<Match>().Select(x => x.Groups["string"].Value).ToArray()));
}
Console.ReadLine();
You regular expression is not correct. In addition, you want to loop through match if you want all matching.
static void Main(string[] args)
{
string input = "Product #_TemplateName_# #_Full_Product_Name_# more text. text text #_Short_Description_#",
pattern = "#_[a-zA-Z_]*_#";
Match match = Regex.Match(input, pattern);
while (match.Success)
{
Console.WriteLine(match.Value);
match = match.NextMatch();
}
Console.ReadLine();
}
Result
Don't use anchors and change your regex to:
(#[^#]+#)
In regex the [^#] expression means any character BUT #
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = #"(#[^#]+#)";
Regex rgx = new Regex(pattern);
string sentence = "#blah blah# asdfasdfaf #somethingelse#";
foreach (Match match in rgx.Matches(sentence))
Console.WriteLine("Found '{0}' at position {1}",
match.Value, match.Index);
}
}
I need to split this code by ',' in C#.
Sample string:
'DC0''008_','23802.76','23802.76','23802.76','Comm,erc,','2f17','3f44c0ba-daf1-44f0-a361-'
I can use string.split(',') but as you can see 'Comm,erc,' is split up by
comm
erc
also 'DC0''008_' should split up as
'DC0''008_'
not as
'DC0'
'008_'
The expected output should be like this:
'DC0''008_'
'23802.76'
'23802.76'
'23802.76'
'Comm,erc,'
'2f17'
'3f44c0ba-daf1-44f0-a361-'
split can do it but regex will be more complex.
You can use Regex.Matches using this simpler regex:
'[^']*'
and get all quoted strings in a collection.
Code:
MatchCollection matches = Regex.Matches(input, #"'[^']*'");
To print all the matched values:
foreach (Match match in Regex.Matches(input, #"'[^']*'"))
Console.WriteLine("Found {0}", match.Value);
To store all matched values in an ArrayList:
ArrayList list = new ArrayList();
foreach (Match match in Regex.Matches(input, #"'[^']*'")) {
list.add(match.Value);
}
EDIT: As per comments below if OP wants to consume '' in the captured string then use this lookaround regex:
'.*?(?<!')'(?!')
(?<!')'(?!') means match a single quote that is not surrounded by another single quote.
RegEx Demo
You can use this Regex to get all the things inside the commas and apostrophes:
(?<=')[^,].*?(?=')
Regex101 Explanation
To convert it into a string array, you can use the following:
var matches = Regex.Matches(strInput, "(?<=')[^,].*?(?=')");
var array = matches.Cast<Match>().Select(x => x.Value).ToArray();
EDIT: If you want it to be able to capture double quotes, then the Regex that will match it in every case becomes unwieldy. At this point, It's better to just use a simpler pattern with Regex.Split:
var matches = Regex.Split(strInput, "^'|'$|','")
.Where(x => !string.IsNullOrEmpty(x))
.ToArray();
it is good to modify your string then split it so that you will achieve what you want like some thing below
string data = "'DC0008_','23802.76','23802.76','23802.76','Comm,erc,','2f17','3f44c0ba-daf1-44f0-a361-'";
data = Process(data); //process before split i.e for the time being replace outer comma with some thing else like '#'
string[] result = data.Split('#'); // now it will work lolz not confirmed and tested
the Process() function is below
private string Process(string input)
{
bool flag = false;
string temp="";
char[] data = input.ToCharArray();
foreach(char ch in data)
{
if(ch == '\'' || ch == '"')
if(flag)
flag=false;
else
flag=true;
if(ch == ',')
{
if(flag) //if it is inside ignore else replace with #
temp+=ch;
else
temp+="#";
}
else
temp+=ch;
}
return temp;
}
see output here http://rextester.com/COAH43918
using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace ConsoleApplication15
{
class Program
{
static void Main(string[] args)
{
string str = "'DC0008_','23802.76','23802.76','23802.76','Comm,erc,','2f17','3f44c0ba-daf1-44f0-a361-'";
var matches = Regex.Matches(str, "(?<=')[^,].*?(?=')");
var array = matches.Cast<Match>().Select(x => x.Value).ToArray();
foreach (var item in array)
Console.WriteLine("'" + item + "'");
}
}
}