Very soring about posting this question, but even if i am a noob in RegEx, i'v started to learn them by myself, but i didn't get the answer for this case.
I am using group, and i wanted to exclude from my result the search caractere i 've used. This is an exemple :
My String : Hand #97407583861: dhihidhi Hand #97407583862: djodjidji Hand #97407583863:
The Regex i use : (?Hand.#\d*:)
The results, i wanted to have in my groups :
97407583861
97407583862
97407583863
97407583864
Best regards
If I understood you correctly, this should help:
string pattern = #"Hand\s#(?<Num>\d+):";
RegexOptions regexOptions = RegexOptions.IgnoreCase | RegexOptions.Multiline;
Regex regex = new Regex(pattern, regexOptions);
string targetString = #"Hand #97407583861: dhihidhi Hand #97407583862: djodjidji Hand #97407583863:";
foreach (Match match in regex.Matches(targetString))
{
if (match.Success)
{
var number = match.Groups["Num"].Value;
}
}
Tested using RegexHero.
Related
Im a fresher to RegEx.
I want to get all Syllables out of my String using this RegEx:
/[^aeiouy]*[aeiouy]+(?:[^aeiouy]*\$|[^aeiouy](?=[^aeiouy]))?/gi
And I implemented it in Dart like this:
void main() {
String test = 'hairspray';
final RegExp syllableRegex = RegExp("/[^aeiouy]*[aeiouy]+(?:[^aeiouy]*\$|[^aeiouy](?=[^aeiouy]))?/gi");
print(test.split(syllableRegex));
}
The Problem:
Im getting the the word in the List not being splitted.
What do I need to change to get the Words divided as List.
I tested the RegEx on regex101 and it shows up to Matches.
But when Im using it in Dart with firstMatch I get null
You need to
Use a mere string pattern without regex delimiters in Dart as a regex pattern
Flags are not used, i is implemented as a caseSensitive option to RegExp and g is implemented as a RegExp#allMatches method
You need to match and extract, not split with your pattern.
You can use
String test = 'hairspray';
final RegExp syllableRegex = RegExp(r"[^aeiouy]*[aeiouy]+(?:[^aeiouy]*$|[^aeiouy](?=[^aeiouy]))?",
caseSensitive: true);
for (Match match in syllableRegex.allMatches(test)) {
print(match.group(0));
}
Output:
hair
spray
Im have a program that vbcan only handle basic regex no C# vb.net etc.
This is my situation.
I have a set of start Urls.
http://www.foo.com?code=234654
I need to remove the ?code= and replace with a / then add the letter t at the end.
Like this:
http://www.foo.com/234654t
I would appreciate any help this this.
Thanks
Sean
For the dialect that is used in java.util.regex you can use this regular expression, for example:
String regex = "\\?+[A-Za-z=]+([0-9]+)(?<=[0-9]+)(?=$)";
String replacement = "/$1t";
Pattern pattern = Pattern.compile(regex);
Matcher m = pattern.matcher(line);
if (m.find()) {
System.out.println(m.replaceAll(replacement));
}
Another example, by using replaceAll:
line.replaceAll("\\?+[A-Za-z=]+", "/").replaceAll("(?<=[0-9|/]+)(?=$)", "t");
For the string:
String line = "http://www.foo.com?code=234654";
You'll get:
http://www.foo.com/234654t
I'm trying to find the words between the brackets.
var str = "asdfasdfkjh {{word1}} asdf fff fffff {{word2}} asdfasdf";
var pattern = /{{\w*}}/g;
var str.match(pattern); // ["{{word1}}","{{word2}}"]
This closes the deal, but gives it with the brackets, and i don't want them.
Sure, if I used the native replace on the results i could remove them. But i want the regexp to do the same.
I've also tried:
var pattern = /(?:{{)(\w*)(?:}})/g
but i can't find the real deal. Could you help me?
Edit: i might need to add a note that the words are dynamic
solution:
Bases on Tim Piezcker awnser i came with this solution:
var arr = [],
re = /{{(\w?)}}/g,item;
while (item = re.exec(s))
arr.push(item[1]);
In most regex flavors, you could use lookaround assertions:
(?<={{)\w*(?=}})
Unfortunately, JavaScript doesn't support lookbehind assertions, so you can't use them.
But the regex you proposed can be used by accessing the first capturing group:
var pattern = /{{(\w*)}}/g;
var match = pattern.exec(subject);
if (match != null) {
result = match[1];
}
A quick and dirty solution would be /[^{]+(?=\}\})/, but it will cause a bit of a mess if the leading braces are omitted, and will also match {word1}}. If I remember correctly, JavaScript does not support look-behind, which is a bit of a shame in this case.
I've been trying to capture the last folder in a folder path using regular expressions in C# but am just too new to this to figure this out. For example if I have C:\Projects\Test then the expression should return Test. If I have H:\Programs\Somefolder\Someotherfolder\Final then the result should be Final. I've tried the below code but it just blows up. Thanks for any help.
string pattern = ".*\\([^\\]+$)";
Match match = Regex.Match("H:\\Projects\\Final", pattern, RegexOptions.IgnoreCase);
Why are you using a regex. You can just use DirectoryInfo.Name
var directoryname = new DirectoryInfo(#"C:\Projects\Test").Name;
\\The variable directoryname will be Test
this is a bad use of regular expressions when you have a pretty complete set of .NET libraries that can do this for you... two easy methods using System.IO.Path or System.IO.DirectoryInfo below
string path = #"H:\Programs\Somefolder\Someotherfolder\Final";
Console.WriteLine(System.IO.Path.GetFileName(path));
Console.WriteLine(new System.IO.DirectoryInfo(path).Name);
Perhaps this?
string strRegex = #".*\\(.*)"; RegexOptions myRegexOptions = RegexOptions.IgnoreCase | RegexOptions.Multiline;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = #"H:\Programs\Somefolder\Someotherfolder\Final";
string strReplace = #"$1";
return myRegex.Replace(strTargetString, strReplace);
Why don't use split?
string str = "c:\temp\temp1\temp2" ;
string lastfolder = str.Split("\").Last ;
Take the following string: "Marketing and Cricket on the Internet".
I would like to find all the possible matches for "Ma" -any text- "et" using a regex. So..
Market
Marketing and Cricket
Marketing and Cricket on the Internet
The regex Ma.*et returns "Marketing and Cricket on the Internet". The regex Ma.*?et returns Market. But I'd like a regex that returns all 3. Is that possible?
Thanks.
As far as I know: No.
But you could match non-greedy first and then generate a new regexp with a quantifier to get the second match.
Like this:
Ma.*?et
Ma.{3,}?et
...and so on...
Thanks guys, that really helped. Here's what I came up with for PHP:
function preg_match_ubergreedy($regex,$text) {
for($i=0;$i<strlen($text);$i++) {
$exp = str_replace("*","{".$i."}",$regex);
preg_match($exp,$text,$matches);
if($matches[0]) {
$matched[] = $matches[0];
}
}
return $matched;
}
$text = "Marketing and Cricket on the Internet";
$matches = preg_match_ubergreedy("#Ma.*?et#is",$text);
Sadly, this is not possible to do with a standard POSIX regex, which returns a single (best candidate, per regex rules) match. You will need to utilize an extension feature, which may be present in the particular programming language in which you are using this regex, assuming that you are using it in a program, to accomplish this task.
For a more general regular expression, another option would be to recursively match the greedy regular expression against the previous match, discarding the first and last characters in turn to ensure that you're matching only a substring of the previous match. After matching Marketing and Cricket on the Internet, we test both arketing and Cricket on the Internet and Marketing and Cricket on the Interne for submatches.
It goes something like this in C#...
public static IEnumerable<Match> SubMatches(Regex r, string input)
{
var result = new List<Match>();
var matches = r.Matches(input);
foreach (Match m in matches)
{
result.Add(m);
if (m.Value.Length > 1)
{
string prefix = m.Value.Substring(0, m.Value.Length - 1);
result.AddRange(SubMatches(r, prefix));
string suffix = m.Value.Substring(1);
result.AddRange(SubMatches(r, suffix));
}
}
return result;
}
This version can, however, end up returning the same submatch several times, for example it would find Marmoset twice in Marketing and Marmosets on the Internet, first as a submatch of Marketing and Marmosets on the Internet, then as a submatch of Marmosets on the Internet.