How to generate strings by regexp in Scala - regex

Suppose there is a simple regular expression (only | and * are allowed). How would you create a stream of strings of any size (from smaller to larger) which match this regular expression in Scala?

Use Xeger. Even though it's Java and not Scala, you should have no problem using it with Scala.
In Java:
String regex = "[ab]{4,6}c";
Xeger generator = new Xeger(regex);
String result = generator.generate();
assert result.matches(regex);

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

How to use regular expression in scala?

I am learning Scala and spark and want to get the numbers out of string.
And for that i am using the regular expression. And came to know about the weird signature of using regular patterns in Scala.
Here is my code:
val myString: String = "there would be some number here 34."
val pattern = """.* ([\d]+).*""".r
val pattern(numberString) = myString
val num = numberString.toInt
println(answer)
The code is working fine, but seems a bit weird and less readable.
Is there any other way to do this in Scala? Or any other syntax which i can use?
The pattern-matching way you are extracting the number is rather resource consuming: since the pattern must match the whole string, you have to add .* on both ends of the regex, and that triggers a lot of backtracking. You also added a space to make sure the first .* does not eat all the digits on the left and return all 1+ digits found.
If you are looking for a first match, use findFirstIn:
val myString: String = "there would be some number here 34."
val numberString = """\d+""".r.findFirstIn(myString)
val num = numberString.get.toInt
println(num) // => 34
See the Scala demo.

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}$

Combining two regular expression

I have these two regular expression ,tried combining .But not working
Dim regExCheckLength As Regex = New Regex("^\w{10}$")
Dim regexCheckFormat As Regex = New Regex("\b(SSN|TC|EMP)")
I am new to reg ex,is there a way to combine
Use logical OR operator to combine both regexes.
Dim regExCheckLength As Regex = New Regex("^\w{10}$|\b(SSN|TC|EMP)")
To satisfy both, you need to use a positive lookahead like below,
Dim regExCheckLength As Regex = New Regex("^(?=\w{10}$).*\b(SSN|TC|EMP).*")

Regex stl not working

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*\\)$")