Regex stl not working - c++

I'm trying to verify if a string correspons to a regex with the stl like this :
regex rgx("#^\([ \r\t\n\f]*([-]?[0-9]+)[ \r\t\n\f]*,[ \r\t\n\f]*([-]?[0-9]+)[ \r\t\n\f]*\)$#");
bool test = regex_search("(12,3)", rgx);
The string is supposed to match, but test = false after that !

You need to wrap your literal to Raw literal:
regex rgx(R"#(^\([ \r\t\n\f]*([-]?[0-9]+)[ \r\t\n\f]*,[ \r\t\n\f]*([-]?[0-9]+)[ \r\t\n\f]*\)$)#");
BTW, I believe the following regexp is identical to yours but much simpler:
regex rgx(R"#(^\(\s*(-?\d+)\s*,\s*(-?\d+)\s*\)$)#");
Or without raw literals:
regex rgx("^\\(\\s*(-?\\d+)\\s*,\\s*(-?\\d+)\\s*\\)$")

Related

Dart RegEx is not splitting String

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

Matching regular expression in Scala

I am trying to test string like {"count":0} in Scala using matches. Since Integer part can be different I am trying to do something like this:
assert(response.matches(s"^\\{\"count\":${notificationCount}\\}$"), s"Actual response: $response")
But I am getting wrong string literal in second $ sign which indicate end of string in regular expression.
Any suggestion?
When using string interpolation in Scala, you can escape $ by using a double $$:
val foo = 5
s"$foo${foo + 1}$$" //56$
You might also consider using a triple-quoted raw string to help with the escaped brace and quote characters:
s"""{"count":${foo}}$$""" //{"count":5}$

Dart: RegExp by example

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&param2=2]
Where <string-of-1-or-more-chars> is just that: any string of 1+ chars, and where the query string ([?param1=1&param2=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

Regular expression problem with back slashes

I'm having trouble with what seems like a simple regex capture. I'm using AutoIt's stringRegExp() function.
The source string is:
1 U:\some text here\more text over here\06-17-2011\Folder 2\161804\abc9831\xyz10007569.JPG
I'm trying to capture "abc9831" and "161804". The "abc" part can be "abc", "def", or "ghi", followed by a string of digits. The "161804" can be replaced with any string of text. Everything is case insensitive. I'm currently using the following regex pattern:
(?i)\\\\.+\\\\((abc\d+)|(def\d+)|(ghi\d+))
But it's only capturing the "abc9831" part. How do I pick up the text string preceding it?
When the regex below is used in AutoIt's StringRegExp() function (using the flag "1" to return an array of matches), it returns 161804\abc9831. Is this what you're wanting to return?
.*\\([^\\]+\\[a-z]{3}\d+)\\.*
Here's an example you can run yourself:
#include <Array.au3>
$string = 'U:\some text here\more text over here\06-17-2011\Folder 2\161804\abc9831\xyz10007569.JPG'
$capture = StringRegExp($string,'.*\\([^\\]+\\[a-z]{3}\d+)\\.*',1)
_ArrayDisplay($capture)
(?i)\\\\(.+\\\\(abc\d+)|(def\d+)|(ghi\d+))
should do the trick if you want it all in one string (with a \ in between).
If you want two separate captures:
(?i)\\\\(.+)\\\\((abc\d+)|(def\d+)|(ghi\d+))
Edit: New version...
The raw regex is \b(\d+)\\((?:abc|def|ghi)\d+). The escaped string is \\b(\\d+)\\\\((?:abc|def|ghi)\\d+)

Stripping out angle brackets with a regular expression

Having the following string:
<str1> <str2> <str3>
I am trying to do the regex such that I get the following 3 strings in C:
str1
str2
str3
I am trying to use the following regex but it doesn't seem to be yielding what I am aiming for:
<[a-zA-Z0-9]*.>
According to http://www.myregextester.com/index.php, that regex is going to yield
<str1>
<str2>
<str3>
How can I take out the <>'s?
Also, I'd also like to ONLY match strings with format, i.e., with 3 <>'s, no more, no less. How to approach that?
This can be easily done with a perl-compatible regular expression like this:
<([^>]+)>
You just have to make sure to tell your library to search for all matches, instead of trying to match the regular expression against the whole string. You will end up with str1, str2 and str3 as the first group match then.
What if you change it for <([a-zA-Z0-9]*.)>
<([a-zA-Z0-9]*?)> - Then you access the second match group of each match. (There is a PHP example on how to access the match group on http://www.myregextester.com/index.php
You can do this with positive lookaround ( http://www.regular-expressions.info/lookaround.html ) like this (?<=<)[a-zA-Z0-9]*(?=>) but not all flavours support it ( http://www.regular-expressions.info/refflavors.html )
Disclaimer: Don't copy Regex from the web if you do not understand how and why it works. This includes answers on Stack Overflow!
This regex would do the job:
/^<([a-zA-Z0-9]*?)>\s<([a-zA-Z0-9]*?)>\s<([a-zA-Z0-9]*?)>$/
example in Perl:
#!/usr/bin/perl
use 5.10.1;
use strict;
use warnings;
my $re = qr/^<([a-zA-Z0-9]*?)>\s<([a-zA-Z0-9]*?)>\s<([a-zA-Z0-9]*?)>$/;
my $str = q!<str1> <str2> <str3>!;
$str =~ s/$re/$1\n$2\n$3/;
say $str;
Output:
str1
str2
str3
You can simple use regex like given below to apply a check on angular brackets.
var text= "<span></span>";
var check = new RegExp(/[^>]\d+[a-z]*[^<])/ );
var valid = check text=
if(!valid) {
return false;
}
else
{
return true;
}