Why my regEx does not work in JScript? [duplicate] - regex

This question already has answers here:
Differences between Javascript regexp literal and constructor
(2 answers)
Javascript RegEx Not Working [duplicate]
(1 answer)
Closed 4 years ago.
I have the following RegEx that works well in Java Script but not in JScript. The only difference I found between the 2 is the that JScript uses /expression/ and tried it with no luck. I need to match specific string date format.
var pattern = "/([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))T(00|[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9])$/";
var regexpattern = new RegExp(pattern);
var str = "2018-02-28T17:05:10";
var res = regexpattern.test(str);
//var res = str.match(pattern);
if ( res != null)
{
Log.Message("Test worked ");
}
else
{
Log.Message("did not");
}
EDIT:
It should be declared as:
var pattern = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))T(00|[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9])$/;

Related

How to get Boolean variable in Regular expressions Swift 4 [duplicate]

This question already has answers here:
How to validate an e-mail address in swift?
(39 answers)
Closed 5 years ago.
I'm trying to make a regular expression that looks for an e-mail. Everything works . How to get a Bool variable that would mean whether such an expression was found or not?
let someString = "123milka#yandex.ru123"
let regexp = "([a-zA-Z]{1,20})#([a-zA-Z]{1,20}).(com|ru|org)"
if let range = someString.range(of: regexp, options: .regularExpression) {
let result : String = someString.substring(with: range)
print(result)
}
You already have an if test, so use that, setting your Boolean as you see fit. There are tons of ways of doing that, e.g.:
let success: Bool
if let range = someString.range(of: regexp, options: .regularExpression) {
success = true
let result = someString.substring(with: range)
print(result)
} else {
success = false
}

Regex match in string [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
I would like to extract two numbers for a strings by regex "[0-9]+"
var str = "ABcDEFG12345DiFKGLSG938SDsFSd"
What I want to extract is "12345" and "938".
But I am not sure how to do so in Kotlin.
This should work:
import java.util.regex.Matcher
import java.util.regex.Pattern
fun main(args:Array<String>) {
val p = Pattern.compile("\\d+")
val m = p.matcher("ABcDEFG12345DiFKGLSG938SDsFSd")
while (m.find())
{
println(m.group())
}
}
Pattern.compile("\\d+"), it will extract the digits from the expression.

RegExp how to deal with RegExp When you call the construcor? [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 6 years ago.
I've different results if I use a RegExp pattern and when I use a new RegExp pattern...
I'm a noob with RegExp.
So
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.display.Graphics;
import flash.text.TextField;
import flash.ui.ContextMenu;
var myString1:String = "Sharsks in the sea";
var pattern1:RegExp = new RegExp("^\s*|\s*$","gim");
var pattern2:RegExp = new RegExp("\s*|\s*$","gim");
var pTest:RegExp = /\s*|\s*$/gim;
var result1:String = myString1.replace(/^\s*|\s*$/gim,"_");
var result2:String = myString1.replace(pattern1,"_");
var result3:String = myString1.replace(/\s*|\s*$/gim,"_");
var result4:String = myString1.replace(pattern2,"_");
var result5:String = myString1.replace(pTest,"_");
trace(result1);
trace(result2);
trace(result3);
trace(result4);
trace(result5);
outputs :
_Sharsks in the sea_
_harsks in the sea_
_S_h_a_r_s_k_s__i_n__t_h_e__s_e_a_
__h_a_r__k__ _i_n_ _t_h_e_ __e_a_
_S_h_a_r_s_k_s__i_n__t_h_e__s_e_a_
Can anyone tell me the difference between new operator and a logical RegExp pattern.
I know that is a very stupid question, so pardon me, but I'm confused...
The RegExp class lets you work with regular expressions, which are patterns that you can use to perform searches in strings and to replace text in strings.
You can create a new RegExp object by using the new RegExp() constructor or by assigning a RegExp literal to a variable:
var pattern1:RegExp = new RegExp("test-\\d", "i");
var pattern2:RegExp = /test-\d/i;
From adobe
In other words, they are equivalent.

Typescript: How to write long regexp in 2 lines [duplicate]

This question already has answers here:
How to split a long regular expression into multiple lines in JavaScript?
(11 answers)
Closed 7 years ago.
I use tslint and when I write long regexp in typescript
var re = /^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
I got error - exceeds maximum line length of 140.
Does anybody know how to write it in 2 lines. I can do that with a hack. But I'm not satisfied with this solution.
var r1 = /^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))/;
var r2 = /#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
var re = new RegExp(r1.source + r2.source);
Why not use strings?
var r1 = "^(([^<>()\[\]\\.,;:\s#\"]+(\.[^<>()\[\]\\.,;:\s#\"]+)*)|(\".+\"))";
var r2 = "#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$";
var re = new RegExp(r1 + r2);
RegExp(string) is easier for modification and/or dynamically generated regex

Getting value using regex [duplicate]

This question already has answers here:
Get values between curly braces c#
(3 answers)
Closed 7 years ago.
how to get the value between first { and last } from a string which have multiple {}.
eg string: ".....[object:{ ..{...{..}...}..}]"
My approach using C#:
line="abcd..efg..[object:{ ab{..c{d.}.e.}f....g}]"
string p = ".*\\[Object:{([A-Za-z{}]*)}\\]";
Regex r = new Regex(p);
Match m=r.match(line);
string value=m.Groups[1].Value.ToString();
Result should be:
value= ab{..c{d.}.e.}f....g
{.*}
or
(?<={).*(?=})
This should do the trick for you.See demo
string strRegex = #"{.*}";
Regex myRegex = new Regex(strRegex, RegexOptions.Multiline);
string strTargetString = #".....[object:{ ..{...{..}...}..}]";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// Add your code here
}
}