I want to extract the value between the apostrophes, for example from this string: package: name='com.app' versionCode='4' versionName='1.3' This is what "aapt" returns when developing android apps. I have to get the values com.app, 4, and 1.3. I'd appreciate any help :)
I found this, however this is VBA.
This regex should work on all cases, assuming that the ' character only occurs as the enclosing character for values:
string input = "package: name='com.app' versionCode='4' versionName='1.3'";
string[] values = Regex.Matches(input, #"'(?<val>.*?)'")
.Cast<Match>()
.Select(match => match.Groups["val"].Value)
.ToArray();
string strRegex = #"(?<==\')(.*?)(?=\')";
RegexOptions myRegexOptions = RegexOptions.None;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = #"package: name='com.app' versionCode='4' versionName='1.3'";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// Add your code here
}
}
RegEx Hero sample here.
In case you're interested, here's a translation of that VBA you linked to:
public static void Test1()
{
string sText = "this {is} a {test}";
Regex oRegExp = new Regex(#"{([^\}]+)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
MatchCollection oMatches = oRegExp.Matches(sText);
foreach (Match Text in oMatches)
{
Console.WriteLine(Text.Value.Substring(1));
}
}
Also in VB.NET:
Sub Test1()
Dim sText = "this {is} a {test}"
Dim oRegExp = New Regex("{([^\}]+)", RegexOptions.IgnoreCase Or RegexOptions.CultureInvariant)
Dim oMatches = oRegExp.Matches(sText)
For Each Text As Match In oMatches
Console.WriteLine(Mid(Text.Value, 2, Len(Text.Value)))
Next
End Sub
Related
I am new to RegEx. I have a string like following. I want to get the values between [{# #}]
Ex: "Employee name is [{#John#}], works for [{#ABC Bank#}], [{#Houston#}]"
I would like to get the following values from the above string.
"John",
"ABC Bank",
"Houston"
Based on the solution Regular Expression Groups in C#.
You can try this:
string sentence = "Employee name is [{#john#}], works for [{#ABC BANK#}],
[{#Houston#}]";
string pattern = #"\[\{\#(.*?)\#\}\]";
foreach (Match match in Regex.Matches(sentence, pattern))
{
if (match.Success && match.Groups.Count > 0)
{
var text = match.Groups[1].Value;
Console.WriteLine(text);
}
}
Console.ReadLine();
Based on the solution and awesome breakdown for matching patterns inside wrapping patterns you could try:
\[\{\#(?<Text>(?:(?!\#\}\]).)*)\#\}\]
Where \[\{\# is your escaped opening sequence of [{# and \#\}\] is the escaped closing sequence of #}].
Your inner values are in the matching group named Text.
string strRegex = #"\[\{\#(?<Text>(?:(?!\#\}\]).)*)\#\}\]";
Regex myRegex = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline);
string strTargetString = #"Employee name is [{#John#}], works for [{#ABC Bank#}], [{#Houston#}]";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
var text = myMatch.Groups["Text"].Value;
// TODO: Do something with it.
}
}
using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Test("the quick brown [{#fox#}] jumps over the lazy dog."));
Console.ReadLine();
}
public static string Test(string str)
{
if (string.IsNullOrEmpty(str))
return string.Empty;
var result = System.Text.RegularExpressions.Regex.Replace(str, #".*\[{#", string.Empty, RegexOptions.Singleline);
result = System.Text.RegularExpressions.Regex.Replace(result, #"\#}].*", string.Empty, RegexOptions.Singleline);
return result;
}
}
}
How to build a regex to remove all commas that are inside quotes(") using C# and then substitute them by #?
Example:
Initial string like this = (value 1,value 2,"value3,value4,value5",value 6)
Expected string like this = (value 1,value 2,"value3#value4#value5", value 6)
You can use
string input = "(value 1,value 2,\"value3,value4,value5\",value 6)";
var regex = new Regex("\\\"(.*?)\\\"");
var output = regex.Replace(input, m => m.Value.Replace(',','#'));
string input = "= (value 1,value 2,\"value3,value4,value5\",value 6)";
string pattern = "(?<=\".*),(?=.*\")";
string result = Regex.Replace(input, pattern, "#");
Regex pattern referred below would work to identify data within double quotes even in multiple level
Regex pattern: ([\"].*[\"])
List<string> input = new List<string>();
input.Add("= (value 1,value 2,\"value3,value4,value5\",value 6)");
input.Add("\"(value 1,value 2,\"value 3, value 4\",value 5,value 6)\"");
var regex = new Regex("([\"].*[\"])");
List<string> output = input.Select(data => regex.Replace(data, m=> m.Value.Replace(',','#'))).ToList();
foreach(string dat in output)
Console.WriteLine(dat);
I am trying to extract the digits from 10:131186; and get 10131186 without the : and ;.
What is the Regex pattern I need to create?
var input = "10:131186;";
string pattern = ":(.*);";
Match m = Regex.Match(input, pattern);
Console.WriteLine(m.Value);
With the above code, I am getting :131186; instead of 10121186.
Why you need to use Regex. It's slower than using string.Replace method
string input = "10:131186;";
input = input.Replace(":", "");
input = input.Replace(";", "");
Console.WriteLine(input);
You can try using Regex.Replace:
var input = "10:131186;";
string pattern = #"(\d+):(\d+);";
string res = Regex.Replace(input, pattern, "$1$2");
Console.WriteLine(res);
and you can also use Split with Join:
var input = "10:131186;";
Console.WriteLine(string.Join("", input.Split (new char[] { ':', ';' }, StringSplitOptions.RemoveEmptyEntries)));
Please try this..
string input = "10:131186;";
input = input.Replace(":", String.Empty).Replace(";", string.Empty);
Just print the group index 1.
var input = "10:131186;";
string pattern = ":(.*);";
Match m = Regex.Match(input, pattern);
Console.WriteLine(m.Value[1]);
or use assertions.
var input = "10:131186;";
string pattern = "(?<=:).*?(?=;)";
Match m = Regex.Match(input, pattern);
Console.WriteLine(m.Value);
You could use the pattern \\d+ to match digits in the string and concatenate them into a single string.
using System;
using System.Text;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string input = "10:131186;";
MatchCollection mCol = Regex.Matches(input, "\\d+");
StringBuilder sb = new StringBuilder();
foreach (Match m in mCol)
{
sb.Append(m.Value);
}
Console.WriteLine(sb);
}
}
Results:
10131186
Demo
I want to convert a string like
m\anoj ku\mar m\a\noj
to
Manoj kUmar MAnoj
how can i do this using c#
string convert(string text)
{
string pattern = #"$1\\";
string repPattern =#"";
string returnText = Regex.Replace(text, repPattern, pattern);
return returnText;
}
What is assigneed to repPattern ? to get result
Try following:
var input = #"m\anoj ku\mar m\a\noj";
var pattern = new Regex(#"([a-z])\\");
var replaced = pattern.Replace(input, m => m.Groups[1].ToString().ToUpper());
Console.WriteLine(replaced);
UPDATE
Map digits to shift-pressed form:
string text= #"m\an1oj ku\mar m\a\no9j";
char[] shiftPressForms = ")!##$%^&*(".ToCharArray();
Regex pattern = new Regex(#"([a-z])\\");
Regex pattern_digit = new Regex(#"\d");
string replaced = pattern.Replace(text, m => m.Groups[1].ToString().ToUpper());
replaced = pattern_digit.Replace(replaced, m => shiftPressForms[int.Parse(m.Value)].ToString());
Console.WriteLine(replaced);
if (Result.Contains("http://"))
{
string pattern = #"(http://)";
theend = Result.Substring(Result.IndexOf("http://"));
Regex rgx = new Regex(pattern);
string replacement = ""+theend+" ";
Result = rgx.Replace(Result, replacement);
}
The result is normal link (a href) and after that there is a string http://. How do I get only a link?
Not clear exactly what are you trying to do. how does the input Result looks like.
if Result only contains the URL then just change:
Result = rgx.Replace(Result, replacement);
to
Result = replacement;
Update:
Anyway, You can use this function:
private string ConvertUrlsToLinks(string msg) {
string regex = #"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:#=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])";
Regex r = new Regex(regex, RegexOptions.IgnoreCase);
return r.Replace(msg, "$1").Replace("href=\"www", "href=\"http://www");
}