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
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
I have an example sentence that looks like this:
""Music"",""EDM / Electronic"",""organizer: Tiny Toons""
I want to parse this sentence into the tokens:
["Music", "EDM / Electronic", "organizer: Tiny Toons"]
My regex foo is quite limited, and I'm under some time pressure.
Was wondering if someone could help me construct a regex (compatible with Java8 - as I'm using Clojure to apply the regex) to parse out these capture groups.
Thank you,
Jason.
Assuming the sentence is the entire string and that there are no commas or " to be matched, you could just use
"[^,\"]+"
If the above assumptions are not correct, please give examples of possible input strings and details of what characters can appear within the sections you want to match.
A simple java example of how to use the regex:
String sentence = "\"\"Music\"\",\"\"EDM / Electronic\"\",\"\"organizer: Tiny Toons\"\"";
Matcher matcher = Pattern.compile("[^,\"]+").matcher(sentence);
List<String> matches = new ArrayList<String>();
while (matcher.find()) {
matches.add(matcher.group());
}
System.out.println(matches);
I'm trying to get my Dart web app to: (1) determine if a particular string matches a given regex, and (2) if it does, extract a group/segment out of the string.
Specifically, I want to make sure that a given string is of the following form:
http://myapp.example.com/#<string-of-1-or-more-chars>[?param1=1¶m2=2]
Where <string-of-1-or-more-chars> is just that: any string of 1+ chars, and where the query string ([?param1=1¶m2=2]) is optional.
So:
Decide if the string matches the regex; and if so
Extract the <string-of-1-or-more-chars> group/segment out of the string
Here's my best attempt:
String testURL = "http://myapp.example.com/#fizz?a=1";
String regex = "^http://myapp.example.com/#.+(\?)+\$";
RegExp regexp= new RegExp(regex);
Iterable<Match> matches = regexp.allMatches(regex);
String viewName = null;
if(matches.length == 0) {
// testURL didn't match regex; throw error.
} else {
// It matched, now extract "fizz" from testURL...
viewName = ??? // (ex: matches.group(2)), etc.
}
In the above code, I know I'm using the RegExp API incorrectly (I'm not even using testURL anywhere), and on top of that, I have no clue how to use the RegExp API to extract (in this case) the "fizz" segment/group out of the URL.
The RegExp class comes with a convenience method for a single match:
RegExp regExp = new RegExp(r"^http://myapp.example.com/#([^?]+)");
var match = regExp.firstMatch("http://myapp.example.com/#fizz?a=1");
print(match[1]);
Note: I used anubhava's regular expression (yours was not escaping the ? correctly).
Note2: even though it's not necessary here, it is usually a good idea to use raw-strings for regular expressions since you don't need to escape $ and \ in them. Sometimes using triple-quote raw-strings are convenient too: new RegExp(r"""some'weird"regexp\$""").
Try this regex:
String regex = "^http://myapp.example.com/#([^?]+)";
And then grab: matches.group(1)
String regex = "^http://myapp.example.com/#([^?]+)";
Then:
var match = matches.elementAt(0);
print("${match.group(1)}"); // output : fizz
Im new to regular expressions and Im trying to use RegExp on gwt Client side. I want to do a simple * matching. (say if user enters 006* , I want to match 006...). Im having trouble writing this. What I have is :
input = (006*)
input = input.replaceAll("\\*", "(" + "\\" + "\\" + "S\\*" + ")");
RegExp regExp = RegExp.compile(input).
It returns true with strings like BKLFD006* too. What am I doing wrong ?
Put a ^ at the start of the regex you're generating.
The ^ character means to match at the start of the source string only.
I think you are mixing two things here, namely replacement and matching.
Matching is used when you want to extract part of the input string that matches a specific pattern. In your case it seems that is what you want, and in order to get one or more digits that are followed by a star and not preceded by anything then you can use the following regex:
^[0-9]+(?=\*)
and here is a Java snippet:
String subjectString = "006*";
String ResultString = null;
Pattern regex = Pattern.compile("^[0-9]+(?=\\*)");
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
ResultString = regexMatcher.group();
}
On the other hand, replacement is used when you want to replace a re-occurring pattern from the input string with something else.
For example, if you want to replace all digits followed by a star with the same digits surrounded by parentheses then you can do it like this:
String input = "006*";
String result = input.replaceAll("^([0-9]+)\\*", "($1)");
Notice the use of $1 to reference the digits that where captured using the capture group ([0-9]+) in the regex pattern.
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 ;