var name = XRegExp('(?<first>\\w+) (?<last>\\w+)','g');
var strNew=XRegExp.replace('John Smith', name, '${last}, ${first}');//->John Smith
why strNew is "John Smith" not "Smith,John"????
It works for me...check you've properly installed XRegExp and are printing out strNew and not name
Related
Having a text contains numbers; which regex expression to use to add numeric separators?
The separator character will be an underscore. Starting from the end, for every 3 digits there will be a separator.
Example input:
Key: 100000,
Value: 120000000000
Expected output:
Key: 100_000,
Value: 120_000_000_000
You can use any popular regex flavor (Perl, Pcre, Python etc.)
(?<=\d)(?=(?:\d\d\d)+\b) will get the positions where to insert the underscore.
Then it is just a matter of injecting the underscore, which is a non-regex task. For instance in JavaScript it would be:
let regex = /(?<=\d)(?=(?:\d\d\d)+\b)/g
let inputstr = `Key: 100000,
Value: 120000000000`;
let result = inputstr.replace(regex, "_");
console.log(result);
And in Python:
import re
regex = r"(?<=\d)(?=(?:\d\d\d)+\b)"
inputstr = """Key: 100000,
Value: 120000000000""";
result = re.sub(regex, "_", inputstr)
print(result)
Regular expressions are used to find patterns in a string. What you do with the matches are language specific.
The regular expression pattern to find three numbers is pretty simple: /\d{3}/
Apply that expression to your specific language to retrieve the matches and build your desired output string:
Perl, using split and then join:
$string = "120000000000"
$new_string = join('_', (split /\d{3}/, $string))
# value of $new_string is: 120_000_000_000
PHP, using split and then join:
$string = "120000000000"
$new_string = implode ("_", preg_split("/\d{3}/", $string))
# value of $new_string is: 120_000_000_000
VB, using split and then join:
Dim MyString As String = "120000000000"
Dim new_string As String = String.Join(Regex.Split(MyString, "\d{3}"), "_")
'new_string value is: 120_000_000_000
Email Masking..
Sample input :- Testuser#gmail.com
Sample output :- T******r#g***.com
First letter of username, 6 asterisks, last letter of username, # character, first letter of domain, 3 asterisks,then extension.
what is the regex expression for this?
I need the code like the way below,
String Email= Email.replaceAll("(?<=.).(?=[^#]*?#)", "*");
Here is a possible Javascript solution
var email = "Testuser#gmail.com"
var re1 = /(?<=[A-Za-z0-9]).+(?=[A-Za-z0-9]\#)/ // Replaces username with "******"
var re2 = /(?<=\#[A-Za-z0-9]).+(?=\.com)/ // Replaces domain with "***"
var modemail = email.replace(re1, "******").replace(re2, "***")
Kapil Arora <kapil.arora#abc.in>
How to find the name before angular bracket
This is the RegEx I used ([^<]+). but it is not finding first String
I would just add a start of input anchor ^ to the head of your expression, plus a look ahead for a space so you get just the name (no trailing space):
^[^<]+(?= )
No need for brackets; group 0 is the whole match, which is what you want.
See live demo.
Since you haven't specified any language. I would be solving in JavaScript.
Lets assume an email id as "kapil.sharma123#gmail.com".
Thus the program would be something like this:
var email = "kapil.sharma123#gmail.com";
var regex = /(^[A-Za-z]+\.+[A-Za-z]+)/;
var res = email.match(regex)[1];
res = res.split(".").join(" ");
Here I am matching the regex with the email id string and then extracting from the first index.
Finally I am spliting on "." and joining with a blankspace.
Note : It also works for simple email ids like "kapil.sharma#gmail.com"
You may try this:
const regex = /^\s*([^<]+)\s*</g;
const str = `Kapil Arora <kapil.arora#abc.in> bla bla bla <asadfasdf>`;
var match = regex.exec(str);
console.log(match[1].trim());
I am trying to just simply disassemble a comma-separated string using the Regex below:
[^,]+
However, I get a different result from this Regex in C# than other engines such as online Regex compilers.
C# for some reason only detects the first element in the string and that's all.
Sample comma-separated string compiled online.
The code I use in C# which returns: Foo
var longString = "Foo, \nBar, \nBaz, \nQux"
var match = Regex.Match(longString, #"[^,]+");
var cutStrings = new List<string>();
if (match.Success)
{
foreach (var capture in match.Captures)
{
cutStrings.Add(capture.ToString());
}
}
Regex.Match returns the first match. Try Regex.Matches to give you the collection of results.
I've been trying to capture the last folder in a folder path using regular expressions in C# but am just too new to this to figure this out. For example if I have C:\Projects\Test then the expression should return Test. If I have H:\Programs\Somefolder\Someotherfolder\Final then the result should be Final. I've tried the below code but it just blows up. Thanks for any help.
string pattern = ".*\\([^\\]+$)";
Match match = Regex.Match("H:\\Projects\\Final", pattern, RegexOptions.IgnoreCase);
Why are you using a regex. You can just use DirectoryInfo.Name
var directoryname = new DirectoryInfo(#"C:\Projects\Test").Name;
\\The variable directoryname will be Test
this is a bad use of regular expressions when you have a pretty complete set of .NET libraries that can do this for you... two easy methods using System.IO.Path or System.IO.DirectoryInfo below
string path = #"H:\Programs\Somefolder\Someotherfolder\Final";
Console.WriteLine(System.IO.Path.GetFileName(path));
Console.WriteLine(new System.IO.DirectoryInfo(path).Name);
Perhaps this?
string strRegex = #".*\\(.*)"; RegexOptions myRegexOptions = RegexOptions.IgnoreCase | RegexOptions.Multiline;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = #"H:\Programs\Somefolder\Someotherfolder\Final";
string strReplace = #"$1";
return myRegex.Replace(strTargetString, strReplace);
Why don't use split?
string str = "c:\temp\temp1\temp2" ;
string lastfolder = str.Split("\").Last ;