Regex Convert from Js to C# [duplicate] - javascript

This question already has an answer here:
Convert JavaScript Regex to C#
(1 answer)
Closed 2 years ago.
How can I convert to following Js regex into C#
let regex = /^([+]?\d{1,2}[.-\s]?)?(\d{3}[.-]?){2}\d{4}$/;
This patterns matches with following international mobile phone numbers
044668180099,
+49-691-234-5678,
+90-537-325-2345,
90-537-566-7152,
I want to do same matches wirh C#
Please advice

Remove '/' at the start and the end of the string.
Regex.Match(yourline, #"^([+]?\d{1,2}[.\-\s]?)?(\d{3}[.-]?){2}\d{4}$");
See also this page about Regex.Match() from the microsoft docs for more overloads.

Related

How can I cleanse a String to contain only digits (0-9) and the letter X (uppercase) only, using Java and Javascript [duplicate]

This question already has answers here:
Remove non-numeric characters except points, commas and '$'?
(2 answers)
Closed 2 years ago.
I am taking an ISBN input by the user - may contain spaces and hyphens etc. - and trying to sanitise it to be only digits.
In Java and Javascript, I have used the following regex successfully
Java (isbn is a java.lang.String)
isbn = isbn.replaceAll("[^\\d]", "");
and, JavaScript
isbn = isbn.replace(/[^\d]/g, "");
However, some ISBNs can have an X as their checksum character. For example, 'The book of days' by Sara Reinke is '155404295X'
How can I change the regex to allow X as well as digits?
Update: [^\dX] worked in JavaScript, but [^\\dX] does not work in Java.
Update 2: PEBKAC! I was sanitising in two places - I updated one but not the other. [^\\dX] does work in Java as well.
Can you try [^0-9X] there? I think it will work in both Java and JavaScript.
P.S. But \\d should work in Java too...
If you wanna follow your solution you can exclude with ?:(values)
For your example it would be: [^\d?:(X)] tested in online java regex.

email regex not working properly with javascript/angular [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 2 years ago.
I have created a regular expression for email validations.
var regex = "^([a-zA-Z]+([\.-_]?[a-zA-z0-9]+)*)\#([a-zA-Z0-9]+)([-][0-9a-z]+)?\.([a-z-]{2,20})(\.[a-z]{2,3})?$"
To match emails:
1. update#update
2. mohit.bhagat#B-9com.com
3. mohit.Bhagat#us.thalesgroup.com
4. mohit#gmail.com.com.com
If you run this over online website 1 and 4th will fail while 2, 3 will pass.
But when I run this code in Javascript( Browser console ), 1st also passes the validation.
I am using Angular application.
The problem is how JS ignores \.
If you do following
var regex = "[a-z]+#[a-z]+\.[a-z]{2,3}"
Resultant string stored is
"[a-z]+#[a-z]+.[a-z]{2,3}"
And if you do this
var regex ="[a-z]+#[a-z]+[\.][a-z]{2,3}"
Resultant string stored is
"[a-z]+#[a-z]+[.][a-z]{2,3}"
Pay attention to [.] with this now i was able to get validation error for 1st email.
Complete regex: "^([a-zA-Z]+([-_.]?[a-zA-Z0-9])*)#([a-zA-Z0-9]+([-][a-z0-9A-Z]+)?)[.]([a-z]+[-_]?[a-z]+)([.][a-z]{2,3})?$"
Update:
Or you can do this: var regex ="[a-z]+#[a-z]+\\.[a-z]{2,3}"
As mentioned in comments below, you can do this way to consider . by including it in [.], but its a hack and not origional way to do it.
Best way to have . included in your regex is \.

how to parse values from comma separated values using regex for javascript [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 5 years ago.
I have
HOSTNAMEA,HOSTNAMEB,HOSTNAMEC,...
I have a third party workflow tool that can do the looping but can only use regex to parse values. I'd like to get a regex that grabs each hostname and puts into it's own variable in my workflow tool so the results will be
HOSTNAMEA
HOSTNAMEB
HOSTNAMEC
...
I'm struggling to get a regex that just grabs the text block X between the commas
ever heard of \w+ if you just want the strings between the comma, you can use .split(", ") as well
var str = "HOSTNAMEA,HOSTNAMEB,HOSTNAMEC";
var res = str.match(/\w+/g);
console.log(res.join(" "));
sample code for your help

Developing a Regex pattern for an email [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 9 years ago.
I have no experience with regular expressions in java script, but I need to derive a pattern for
FMLast1234#ung.edu. There only needs to be a pattern for FMLast1234 because #ung.edu needs to remain the same. I am not sure if I just do this \b[a-z][a-z][a-z][0-9] or what.
Does there need to be a range for each character in the pattern? I need to check for variations of the pattern FMLast1234 not just a random assortment of characters and numbers.
/[a-zA-Z0-9]#ung.edu/.test("123#ung.edu") or
if(emailString.split('#')[1] === "ung.edu")
Edit : As per plalx comment here is my answer
/^\w+#ung.edu$/.test("aaa123#ung.edu")

Ignoring line breaks in Javascript regex [duplicate]

This question already has answers here:
JavaScript regex multiline text between two tags
(5 answers)
Closed 4 years ago.
Even if I use the m flag, javascript regex seems to isolate regex matching by lines.
Example:
"if\nend".match(/if(.*?)end/m)
=> null
I want this to match. How do I get around this?
You actually want s (a.k.a. "dotall"), not m, but javascript doesn't support that. A workaround:
"if\nend".match(/if([\s\S]*?)end/)

Categories

Resources