Regular Expressions

A regular expression ("regex") is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. "find and replace"-like operations. Regular expression processors are found in several search engines, search and replace dialogs of several word processors and text editors, and in the command lines of text processing utilities, such as sed and AWK. Many programming languages provide regular expression capabilities, some built-in, for example Perl, JavaScript, Ruby, AWK, and Tcl, and others via a standard library, for example .NET languages, Java, Python and C++ (since C++11). Most other languages offer regular expressions via a library.

Share tech news, updates, or what's on your mind.

Sign up to Post

I would like to do a function in SQL that uses a regex string to validate email addresses and which also is not going to bog things down.
0
Exploring ASP.NET Core: Fundamentals
LVL 12
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Hello,

I need help writing a RegEx for my alerting system (PagerDuty).  It essentially works when it is forwarded emails from our network monitoring equipment.  I am trying to setup a rule so that alerts get tagged and closed correctly.  The rules look at the subject of a message which will look like the following:

switch.core: minor service affecting Alarm for DEVICE "119" at 2019/01/04 11:45:25.27: "DEVICE went missing"

In that subject, I would be looking to open the alert if "minor service affecting" and "DEVICE went missing" was matched in the subject.  I have this working by using the expression:

/(minor service affecting|DEVICE went missing)/ms

Next a second email might come through which would be the resolution of this problem and would have a subject which looks like the following:

switch.core: clear service affecting Alarm for DEVICE "119" at 2019/01/04 11:45:25.27: "DEVICE went missing"

In this rule I have the ability to match emails based on a regular expression.  It would need to match with the initial email and shares 3 keys as follows:

"switch.core", "119", and "DEVICE went missing".  How would I write a regular expression that would pull those 3 fields out?  The 3 fields would always be in the same order in both subjects and the subject format would always be the same.  So I guess I need a RegEx that would extract a match searching for those 3 items that would work against both subjects.  I should note that the values I have used may …
0
what is the perl regular expresssion to compare "machinename\\username" in perl scripts?

sample code:
print "Hello World!\n";
$mname="BVM";
$user = "test";
$mu_reg = "^(?i)$mname/\/\$user \$";
$mu_str = "164AUTH/\/\chris";



$ipstr="$mu_str ";
$ipstr_re = "^(?i)mu_reg \$"; # regex failed with two backslashes. How to fix it?
if ($ipstr !~ /$ipstr_re/) {
    print "It doesn't match\n";
}
else {
    print "It matches\n";
}
0
I have an input field that requires a user to enter time in a decimal form of hours + minutes. I need an html validation regex that will force a user to only enter the decimal 0 or 5. For example, user can enter the following valid numbers: (10.0; 10.5; 11.0; 3.5; 2.0) and so on. Invalid numbers:( 10.1; 10.2; 10.4; 3.2; 4.4;7.7). I just need to ensure user enters .0 or .5 decimal  no greater than 99.5, and great than 0.
1
I need help with regx expression below.  Currently it will only find numeric decimal values but I need both a integer check and numeric decimal check.  

.*?([\-\+]?(\d+\.\d+KW|\d+|\.\d+))\D*

For example:

Input Data                                                            Result
Dual Rated - 240V/3.5KW & 208V/2.5KW    = 3.5KW   --> this works now.
Dual Rated - 240V/5KW & 208V                    = 5KW      --> Expression above does not detect integer values.
5KW                                                                   = 5KW      --> Expression above does not detect integer values.
5.6KW                                                                = 5,6KW
0.0                                                                      = [null]
5                                                                          = [null]
0
A configuration file that has lines as below:

<abcde=12 bcde=20 xyz=30 match=pattern1>

Open in new window


there are several such lines; some with "match=pattern1", as above. There are other lines where they could be "match=pattern2", "match=pattern3" and so on.

I would like to modify lines that have "pattern1" ONLY. Again, there's more than one line that has "pattern1".

I would like to change bcde=99 and xyz=99 where pattern1 is present.

How can this be done?
0
I need some mod rewrite help with a regex.  This is an example url - https://www.thefrugallife.com/12all/lt.php?c=1728&m=2981&nl=1&s=c9b91a7ca200a96e668e515ecf49b34c&lid=19221&l=-http--www.theherbsplace.com/Shop_A_Z_page_1_c_28.html

I am trying to get it to redirect to the url after the last equals sign - http--www.theherbsplace.com/Shop_A_Z_page_1_c_28.html

I have 2,000 links like this all with a different url after the last equals sign.

Here is my code

RewriteCond %{REQUEST_URI} /?12all/lt\.php$
RewriteCond %{QUERY_STRING} c=(.*)$ [NC]
RewriteCond %{QUERY_STRING} m=(.*)$ [NC]
RewriteCond %{QUERY_STRING} nl=(.*)$ [NC]
RewriteCond %{QUERY_STRING} s=(.*)$ [NC]
RewriteCond %{QUERY_STRING} lid=(.*)$ [NC]
RewriteCond %{QUERY_STRING} l=-http--(.*)$ [NC]
RewriteRule ^.*$ https://%6/? [NC,L,R=301]

Open in new window


Please tell me what I am doing wrong so I can get this cleaned up for Google.

Thanks,
0
HI.

I am using Microsoft Access VBA in a Windows 10 machine.
I am trying to extract data from a multi-line string copied from a web page whose format is always the same.

By example , one of the lines in the web page would be

Circuit Name:           <data item 1>            Issued by:
Customer Name      <data item 2>            Attn

I would like the regex pattern that would give me the  <data item 1>   ..   Also,  <data Item 2>

Thank you.
1
Given an array or collection of words as below. I want to find each word in the string text and replace it with say an empty string .



var words = new string[] { "apple", "cat", "red" };

var text = "I have a red apple and a small cat";        
         

            foreach (var w in words) {

                output = Regex.Replace(text, @"\b" + w + @"\b", " ");
                text = output;
             
            }

is there a better way to do this?

also, if I have an array of special characters. If any of the character is found , it will be replaced by an empty string.
In my example below, I have $$$, since it is the same character $ , appearing multiple of times, I would just like to replace my one empty string character,
how do i achieve this?

 Regex reg = new Regex("[*&#$^@{}]");
 var result = reg.Replace("I have * , as well as $$$" , " ");
0
What would be the Javascript regex to only allow sub-domains of:
  • we.abc.com
  • wesource.abc.com
  • we-uat.abc.com
  • we-qa.abc.com

Thanks!
0
Exploring SharePoint 2016
LVL 12
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Hello,

I'm using Notepad++ to test some REGEX against a csv file.  

I've got a string that seems to be correctly identifying the first instance of a space followed by an upper case letter in each row of the file:

(^.*?)\s[A-Z]

The next step would be to insert a pipe symbol just before the uppercase letter.

So given a line like this:
XAb(2)-AB SOME UPPERCASE TEXT | SOME OTHER STUFF | AND MORE STUFF

The REGEX would replace the pattern with:
XAb(2)-AB |SOME UPPERCASE TEXT | SOME OTHER STUFF | AND MORE STUFF

Any help will be greatly appreciated!
0
Hi,

I want to know if there is a way that I can test the regex online that are created for Keyword Highlighting in SecureCRT. Below is the sample regex entries that I want to test and modify as per my requirement.

 "[*]For_Best_Results:_Set_Forground_Font_To_Gray_(Not_Black!)"
 "[*]Gray_Keywords_Are_To_Work_Around_Symbol_Delimited_Words"
 "[*]Special_Cases_That_Had_To_Be_ReOrderd"
 ".*'.*"
 "on-fail.*
 "[*]In_Enable_Mode"
 "^\w[^>]*#|hostname"
 "[*]NOT_In_Enable_mode"
 "^\w[^#]*>"
 "[*]Interfaces"
 "Embedded-Service-Engine\d\/\d"
 ".*thernet[0-9]+(?:[\/\.:][0-9]+)+[,:]?(?:\x20|$)"
 ".*thernet[0-9]+[,:]?(?:\x20|$)"
 "\b[efgt][a-z]*[0-9]+(?:[\/.:][0-9]+)+[,:*]?(?:\x20|$)"

Open in new window

1
I need a regular expression that will identify values that end with a plus sign.  I have a column that contains multiple weights in some cases and when it does I just need to extract the second value.  For example:

Values
607+800 lb    --> I need to identify the 607+ and remove.
822 + 44 lb    --> I need to identify the 822 + and remove.
555 lb
100 lb
0
I need a regular expression to check for a word in a string that has a "/".  

Value
---------
40005 Btu/h    ---->  I need to detect if the value has "Btu/h"
5009 BTU/H    ---->  I need to detect if the value has "BTU/H"
6999 BTU        -----> I also need to detect if value has just "BTU".
6700
900000 Btu
0
I need help with a regular expression that will check a column for a certain word, blanks and all white space.  I need a regx to check If the column below contains the word "NULL" or "Null" or  is blank or contains just white space.  

Description
----------------
NULL                  ------> If the column contains the word NULL.  
Plates
                             ----> White space example.  
Cups
Napkins and Cups
                           ------> Blank example
0
Experts,

Using C# I need to pull a date out of a string. The date format will always be numbers in day/month/year style. Of course the day and months could be 1 or 2 digits each, the years are always 4 digit. I've been reading about regex's but i can't seem to get it right. The string contains lots of words in addition to the date. The date always has at least one space leading and trailing it.
Example: "Here we 7/15/1957           down the path"

Thanks!
0
Hello,

I have over 100 CSV files that I need to pre-process before importing to SQL.  The files are too large to open in Excel.

What I'm trying to do is modify the first line (the header row) of each file to add double-quotes to the field terminators.

For example, given this header row:
field1|field2|field3|field4

replace | with "|" so the first row would be:
field1"|"field2"|"field3"|"field4

Notepad++ has a "find in files" feature that can use regex but I'm not sure how to linit it to the first line.

Any ideas would be greatly appreciated.  

Thanks!
0
Hello,
I'm trying to clean a pipe delimited text file in notepad++ (or any other tool that would do the job quickly).  

Here's an example of the file layout:

col1|col2|col3|                                                             [cr][lf]
"value"|"vlaue"|"value"|                                             [cr][lf]
"value"|"vaaaalue"|"vaaaalue"|                                [cr][lf]


What I hope I conveyed visually is:
  • the file has a "faux" column after col3
  • there are varying lengths of white space between the carriage return and line feed of each row
  • the values in each column are of varying lengths

For each line I would like to remove the last pipe and any whitespace between the last pipe and the CRLF.
I can open the file in Notepad++ which has a REGEX find/replace function but I"m not exactly sure how to compose the search.

The file is too large to open in Excel.  

Any help will be greatly appreciated.  

TIA!
0
I need to create some regex criteria to assist with a custom content filter in some eDiscovery software, to filter a 100 GB directory of data for any documents which contain a certain pattern of text to meet the criteria 9 characters long, first two characters any alpha (a-z), characters 3-8 any numeric characters, the final character any alpha (a-z). The alpha characters are likely to be uppercase but not always. Any sort of starting point will help as very new to regex
1
C++ 11 Fundamentals
LVL 12
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

I am having a little trouble with regex. Here is what I want:

xxx123.xxx123@xxx123.xxx123 should be found
xxx123@xxx123.xxx123.xxx123 should be found
xxx123.xxx123.xxx123@xxx123 should be found

xxx123.xxx123@xxx123.no should not be found
xxx123.xxx123@xxx123.xxx123.com should not be found
xxx123.xxx123.xxx123@xxx123.xxx123.org should not be found

In other words I want to find all email adresses missing the the .no, .com, .it, .org, .net etc. end part.

I am sure it is simple, but I haven't quite figured it out, so I would really appreciate any help on this!

Thanks!

Brgds
IVer in Oslo
1
On my PC this location contains a bunch of XML files

C:\SmartCompletion\Backend\IBS.Core\Resources

I'd like to limit my search to **just** the Resources folder.

There is a particular tag inside these XML files that I want to look at and verify that it does NOT contain a certain attribute:

 <Layout mode="Complex" UsePurgeRestore="true" UseCopy="true">   //////////// this would not show up in the search results

UsePurgeRestore="true" or UsePurgeRestore="false"  must NOT be an attribute in the <Layout> tag

So this would be a match for my search:

<Layout mode="Complex" UseCopy="true">

or this:

  <Layout UseAssignment="true" mode="Complex"  EditNewView="vAssetPacksLines">


Can Visual Studio do this using Regular Expressions?

I would need the RegEx.  I'm too slow at it and I need this as fast as I can.

reg ex search

NOTE:  I am open to freeware tools that can do this kind of exclusionary search of file contents.  I have AgentRansack, for example.  I also have Notepad++.   Do either of those support RegEx searches?

Thank you!
0
What's the JS regex for the below where I can have "we" and then possibly some characters followed by ".bank.com?:


wedev.bank.com
weqa.bank.com
weuat.bank.com
westage.bank.com
we.bank.com

Thanks!
0
Hello,

I am trying to write a regular expression for a spam filter that checks for  @  appearing more than once in a string.

Reason :  there is a new type of file-less virus exploit that comes into our spam filter  with two email addresses in the FROM field.  The writers of the spam filter are working on producing a fix but in the meantime I want to write a rule which examines the FROM field and if it finds  more than 1 instance of  "@"  in the string that is the FROM field, then the rule will reject that email.

Thanks for any help on this.

M.
2
I need SED to replace a string across multiple ASCII text files based on a regex pattern.  These files are descriptions of EDI file formats with records described in this format:

E2EDPT2 {  DELIMITER="\x0a" }:
    E2EDPT2_SEGNAM		# Segment E2EDPT2
    E2EDPT2_MANDT
    E2EDPT2_DOCNUM
    E2EDPT2_SEGNUM
    E2EDPT2_PSGNUM
    E2EDPT2_HLEVEL
    E2EDPT2_TDLINE
    E2EDPT2_TDFORMAT
    rest[0:"^\x0a"]*
;

E2EDPT2_SEGNAM: STRINGA { LENGTH=30, DEFAULT="E2EDPT2001                    " };
E2EDPT2_MANDT: NSTRINGA { LENGTH=3 };
E2EDPT2_DOCNUM: STRINGA { LENGTH=16 };
E2EDPT2_SEGNUM: STRINGA { LENGTH=6 };
E2EDPT2_PSGNUM: STRINGA { LENGTH=6 };
E2EDPT2_HLEVEL: STRINGA { LENGTH=2 };
E2EDPT2_TDLINE: STRINGA { LENGTH=70, MISSVALUE="                                                                      " };
E2EDPT2_TDFORMAT: STRINGA { LENGTH=2, MISSVALUE="  " };

Open in new window


What I need to do is replace the occurrences of fields like "E2EDPT2_SEGNUM" above with plain "SEGNUM".  All record types will have this field and I want to lose the record name part leaving just the fieldname SEGNUM.  The recordname part will always begin with a fixed length "E2EDXXX-".  What is the correct regular expression to get this done?
0
Hi Experts,

I have a string like below:

input = Test_8234_and_2345_end

I'm trying to create a regex that targets only the first group of 4 digits (i.e 8234) in the above string but it returns none. Underscore ('-') may be/may not be there in the input. what is wrong with my below code.
REG_EXTRACT(input,'(\d+)',1)
0

Regular Expressions

A regular expression ("regex") is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. "find and replace"-like operations. Regular expression processors are found in several search engines, search and replace dialogs of several word processors and text editors, and in the command lines of text processing utilities, such as sed and AWK. Many programming languages provide regular expression capabilities, some built-in, for example Perl, JavaScript, Ruby, AWK, and Tcl, and others via a standard library, for example .NET languages, Java, Python and C++ (since C++11). Most other languages offer regular expressions via a library.