Regular expression in Flex - regex

I want to check if the string is not empty (having whitespaces only also counts as empty). How to compose the regular expression in actionscript?

The pattern should be something like /^\s*$/ (for a single line string); ^ and $ represent the start and end of the line and \s* means match zero or more whitespace characters. For example:
var s:String = /* ... */;
var allWhitespaceOrEmpty:RegExp = /^\s*$/;
if (allWhitespaceOrEmpty.test(s))
{
// is empty or all whitespace
}
else
{
// is non-empty with at least 1 non-whitespace char
}
Perhaps a simpler way as commenter Alexander Farber points out is to check for any character except a whitespace character, which is matched by \S in regex:
var nonWhitespaceChar:RegExp = /\S/;
if (nonWhitespaceChar.test(s))
{
// is non-empty with at least 1 non-whitespace char
}

Related

Why does the regex [a-zA-Z]{5} return true for non-matching string?

I defined a regular expression to check if the string only contains alphabetic characters and with length 5:
use regex::Regex;
fn main() {
let re = Regex::new("[a-zA-Z]{5}").unwrap();
println!("{}", re.is_match("this-shouldn't-return-true#"));
}
The text I use contains many illegal characters and is longer than 5 characters, so why does this return true?
You have to put it inside ^...$ to match the whole string and not just parts:
use regex::Regex;
fn main() {
let re = Regex::new("^[a-zA-Z]{5}$").unwrap();
println!("{}", re.is_match("this-shouldn't-return-true#"));
}
Playground.
As explained in the docs:
Notice the use of the ^ and $ anchors. In this crate, every expression is executed with an implicit .*? at the beginning and end, which allows it to match anywhere in the text. Anchors can be used to ensure that the full text matches an expression.
Your pattern returns true because it matches any consecutive 5 alpha chars, in your case it matches both 'shouldn't' and 'return'.
Change your regex to: ^[a-zA-Z]{5}$
^ start of string
[a-zA-Z]{5} matches 5 alpha chars
$ end of string
This will match a string only if the string has a length of 5 chars and all of the chars from start to end fall in range a-z and A-Z.

Kotlin .split() with multiple regex

Input: """aaaabb\\\\\cc"""
Pattern: ["""aaa""", """\\""", """\"""]
Output: [aaa, abb, \\, \\, \, cc]
How can I split Input to Output using patterns in Pattern in Kotlin?
I found that Regex("(?<=cha)|(?=cha)") helps patterns to remain after spliting, so I tried to use looping, but some of the patterns like '\' and '[' require escape backslash, so I'm not able to use loop for spliting.
EDIT:
val temp = mutableListOf<String>()
for (e in Input.split(Regex("(?<=\\)|(?=\\)"))) temp.add(e)
This is what I've been doing, but this does not work for multiple regex, and this add extra "" at the end of temp if Input ends with "\"
You may use the function I wrote for some previous question that splits by a pattern keeping all matched and non-matched substrings:
private fun splitKeepDelims(s: String, rx: Regex, keep_empty: Boolean = true) : MutableList<String> {
var res = mutableListOf<String>() // Declare the mutable list var
var start = 0 // Define var for substring start pos
rx.findAll(s).forEach { // Looking for matches
val substr_before = s.substring(start, it.range.first()) // // Substring before match start
if (substr_before.length > 0 || keep_empty) {
res.add(substr_before) // Adding substring before match start
}
res.add(it.value) // Adding match
start = it.range.last()+1 // Updating start pos of next substring before match
}
if ( start != s.length ) res.add(s.substring(start)) // Adding text after last match if any
return res
}
You just need a dynamic pattern from yoyur Pattern list items by joining them with a |, an alternation operator while remembering to escape all the items:
val Pattern = listOf("aaa", """\\""", "\\") // Define the list of literal patterns
val rx = Pattern.map{Regex.escape(it)}.joinToString("|").toRegex() // Build a pattern, \Qaaa\E|\Q\\\E|\Q\\E
val text = """aaaabb\\\\\cc"""
println(splitKeepDelims(text, rx, false))
// => [aaa, abb, \\, \\, \, cc]
See the Kotlin demo
Note that between \Q and \E, all chars in the pattern are considered literal chars, not special regex metacharacters.

c# Regex expression to extract all non-numeric values in brackets

This is the Regex expression i have built so far \{([^{]*[^0-9])\}.
"This is the sample string {0} {1} {} {abc} {12abc} {abc123}"
I wish to extract everything within the string that includes brackets and that does not contain only an integer.
(e.g) '{}'
'{abc}' '{12abc}' '{abc123}'
However the last one which contains numbers at the end is not extracted with the rest.
{abc123}
How can i extract all values in the string that are in curly brackets and do not contain an Integer?
You may use
var res = Regex.Matches(s, #"{(?!\d+})[^{}]*}")
.Cast<Match>()
.Select(x => x.Value)
.ToList();
See the regex demo and the online C# demo.
Pattern details
{ - a { char
(?!\d+}) - no 1+ digits and then } allowed immediately to the right of the current location
[^{}]* - 0+ chars other than { and }
} - a } char.

Detect word that end with a semicolon with regex

am tyring to write a regex pattern that detects if a word starts with var and ends with a semicolon. I have the regex code in place but when run it matches from where the last semicolon is instead of matching each word ending with a semicolon. here is my regex pattern.
dim pattern as string = "^[var].+;$"; help please.
your regex now matching whole line which starts with 'v', 'a' or 'r' and ends with semicolon.
if you want match whole lines start with var and ends with semicolon this is the way:
"^var.+;$"
if only variable definitions inside line then:
"var.+;"
this second way will match following:
var a;
var b, c;
a = 5; var b, c = a; //comment
a = 5; //comment var ;
bold indicates match
Seems like you want something like this,
(?<!\S)var\S*;(?!\S)
(?<!\S) Asserts that the match var won't be preceded by a non-space character. \S* matches zero or more non-space characters. ;(?!\S) Matches the semicolon only if it's not followed by a non-space character.
DEMO
OR
(?<!\S)var.*?;(?!\S)
DEMO

How to validate a string to have only certain letters by perl and regex

I am looking for a perl regex which will validate a string containing only the letters ACGT. For example "AACGGGTTA" should be valid while "AAYYGGTTA" should be invalid, since the second string has "YY" which is not one of A,C,G,T letters. I have the following code, but it validates both the above strings
if($userinput =~/[A|C|G|T]/i)
{
$validEntry = 1;
print "Valid\n";
}
Thanks
Use a character class, and make sure you check the whole string by using the start of string token, \A, and end of string token, \z.
You should also use * or + to indicate how many characters you want to match -- * means "zero or more" and + means "one or more."
Thus, the regex below is saying "between the start and the end of the (case insensitive) string, there should be one or more of the following characters only: a, c, g, t"
if($userinput =~ /\A[acgt]+\z/i)
{
$validEntry = 1;
print "Valid\n";
}
Using the character-counting tr operator:
if( $userinput !~ tr/ACGT//c )
{
$validEntry = 1;
print "Valid\n";
}
tr/characterset// counts how many characters in the string are in characterset; with the /c flag, it counts how many are not in the characterset. Using !~ instead of =~ negates the result, so it will be true if there are no characters not in characterset or false if there are characters not in characterset.
Your character class [A|C|G|T] contains |. | does not stand for alternation in a character class, it only stands for itself. Therefore, the character class would include the | character, which is not what you want.
Your pattern is not anchored. The pattern /[ACGT]+/ would match any string that contains one or more of any of those characters. Instead, you need to anchor your pattern, so that only strings that contain just those characters from beginning to end are matched.
$ can match a newline. To avoid that, use \z to anchor at the end. \A anchors at the beginning (although it doesn't make a difference whether you use that or ^ in this case, using \A provides a nice symmetry.
So, you check should be written:
if ($userinput =~ /\A [ACGT]+ \z/ix)
{
$validEntry = 1;
print "Valid\n";
}