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

Dear Experts

I am trying to find software - optimally free, which would allow me to play with regular expression in a way, that from current set of found capturing groups I would be able to setup new text, copy it and paste where I need.

Simply example:

I have 2 lines of text taken from somewhere (copied) and pasted into Regular expression software:
2018-09-18 Hash XY
2019-01-17 Hash AB


In search field I would search for (\d\d\d\d)-(\d\d)-(\d\d)

And somewhere in 3rd window I wanted to enter %3-%2-%1

Which would give me
18-09-2018
17-01-2019

Do you have any idea?

Thanks

Vladimir
1
Build an E-Commerce Site with Angular 5
LVL 12
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Hi there, I need a regex expression to extract a uk phone number from a string.
This is with a view to detecting phone numbers in paragraphs of text.
There are plenty of examples out there to verify UK phone numbers as being a regex match but I cannot find anything which will do so returning the match from within a string.
Uk phone numbers are 10 or 11  numbers long with a 0 at the beginning.  

This regex is an example of a regex which matches a phone number, but cannot handle leading or trailing text.  Can somebody help perhaps in modifying it so it can still pick out the numbers in a string whilst ignoring leading/trailing text.  

^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$

Open in new window


Thanks,
0
How to have textbox regular expression validation on asp.net?


I will have to allow 3 digits and units in the text box. Decimal units should not exceed more than 2 units.

How to handle that?
0
I'm looking for a  Javascript regx that will detect a-z (case insensitive)  or 09 as a group and then a hyphen followed by any group  of a-z or 09  etc. Always will start/end with (a-z or 09) For example:

a12dd3-23-fff-11n3N33n3-988
9fkr-rtt488-344-45
r5999999Rrp-4-yei388dfi3-3444-fjj577-23
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
1234-567-8-922-222222-2-1111
0
From the following extract from a source input stream, i need to use c# regex to extract a match collection of each matching block of code between the ##if WORD ## and the matching ##endif WORD ##

##if textbox ##
		<tr>
			<td>Textbox description here, could be anything:</td>
			<td>##textbox##</td>
		</tr>
##endif textbox ##
		<tr>
			<td>multilinetextbox</td>
			<td>##multilinetextbox##</td>
		</tr>
##if staticdropdown ##
		<tr>
			<td>staticdropdown description here, could be anything or nothing or something else alltogether:</td>
			<td>##staticdropdown##</td>
		</tr>
##endif staticdropdown ##

Open in new window


The returned match should include the ##if and end ##endif blocks as below...

Match 1:
##if textbox ##
		<tr>
			<td>Textbox description here, could be anything:</td>
			<td>##textbox##</td>
		</tr>
##endif textbox ##

Open in new window


Match 2:
##if staticdropdown ##
		<tr>
			<td>staticdropdown description here, could be anything or nothing or something else alltogether:</td>
			<td>##staticdropdown##</td>
		</tr>
##endif staticdropdown ##

Open in new window


I have been testing various online regex testing sites to see I I can get what I need but always end up with the match being from the very first ## if WORD ## to the very last ##endif OTHERWORD ## construct

Any help gladly accepted :D
0
Regex Destination url needs to be lower case.  I found this code that will convert it to lower case in the httpd.conf file.

RewriteMap lc int:tolower
RewriteRule (.*?[A-Z]+.*) ${lc:$1} [R]

Open in new window


Here is a sample url

http://www.theherbsplace.com/Garlic_Oil_Capsules_p_400.html

Open in new window

source url

https://www.theherbsplace.com/product/garlic_oil_capsules_p_400/

Open in new window

target url

I need to know if there is a better way to do this.  Because it seems very slow on Xampp.
0
linux shared server
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301] 

Open in new window


this is .htaccess code (i hope it is correct)
to have www. redirect to non www

sometimes I type wwwww.example.com or ww.example.com
could I have a rule where all subdomains forward to http://example.com
0
JavaScript, Regular-expression, Replace

I am trying to use a regular expression to correct an HL7 msg.
Using 'replace' to search the inbound text message and find segments that are not correctly terminated w/carriage returns.

Example:  // this works
var msg = "MSH|...|...|\rGT1|...|...|GT1|...|...|\r";
msg = msg.replace(/\|GT1\|/g, "|\rGT1|");

Open in new window

// This leaves me with:
MSH|...|...|\r
GT1|...|...|\r
GT1|...|...|\r

Open in new window

// but, I don't always know that the 2nd instance of 'GT1' will be proceeded w/a '|' (pipe) character.
How do I search for any character in the 1st (search) argument and get it into the 2nd (replace) argument ?
var msg = "MSH|...|...|\rGT1|...|...|GT1|...|...|\r";
msg = msg.replace(/.GT1\|/g, ".\rGT1|");
// What goes here  ^ and Here ^

Open in new window

I need to avoid adding a double \r\r !

Thank you.
0
I need some coding help... I have a string, for example:

$string = "post-title/367895378/";

The numbers there... in the string they always start with 367 and then there are anywhere from 3 to 6 digits afterwards.

I need to remove the "367" and all the numbers afterwards, and the trailing slant, so that the string ends up as "post-title/" (the post title changes based on the page it's on, of course).

The numbers starting with 367... and the slant are always going to be at the end of the url, so everything starting with 367 and afterwards should be eliminated.

I know that needs some kind of regex, but that's beyond my skills...

Thanks,

Chris
0
I need to remove an encoded ? (%3F) off of several url's.  I want to use Mod_Rewrite to accomplish that.  Here is my regex but it itsn't working.

RewriteRule ^index\.php\?controller=cms&id_cms=([0-9]+)%3F$ /index.php?controller=cms&id_cms=$1 [R=301,L]

Open in new window


This is what the original url looks like
/index.php?controller=cms&id_cms=73%3F

Open in new window


Please tell me how to get rid of the %3F appended to the url.
0
Become a CompTIA Certified Healthcare IT Tech
LVL 12
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.

In an effort to streamline and systematize error message that are presented to the user from the application system we are developing/sustaining, I have made a complete list of error message and where they are found in the code base.

Except for error messages that origin from views and stored procedures in the MSSQL database that is the backend for this system.

When I searched for error messages in the application code base, I used regular expressions to look for hardcoded error messages in the source code. I also manually looked through all .resx files for error messages. But I am not sure of what is the best approach when searching for error messages in views and stored procedures.

The amount of views and stored procedures in the database backend is huge, and it will take an immense amount of time to read through it all manually. I also don't trust that I will catch all error messages sufficiently by manually reading all the code.

So, are there any good ideas on how to approach this task of finding error messages in views and stored procedures in an MSSQL database in a quick as possible and efficient way?
0
I'm trying to extract a list of parameter names from a Python script. Here's an example of what I'm looking at
def foo(cmd
    ,pIncludeAll #BOOL
    ,pOrderByDisplayOrder #BOOL
    ) :
    try:
        ....

Open in new window

Currently, I'm using a Regex that grabs everything between the parentheses then just splitting on the comma. This doesn't work in the above case since there are comments after 2 of the 3 parameters. My split string ends up looking like this:
cmd
pIncludeAll #BOOL
pOrderByDisplayOrder #BOOL

Open in new window

What I need is a Regex that will produce a match result that contains each of the parameters without the comment like this:
cmd
pIncludeAll
pOrderByDisplayOrder

Open in new window

I know I need to delimit the Regex match on commas, whitespace, and pound signs. I just don't know how to write the expression so that it will return a proper match against an arbitrary number of arguments.
0
Need to capture the "3" value using REGEX in the following  string:

"0338829990|10881|188892|ELRET|","","3","","2018-08-23T09:55:23-04:00"
0
I have a txt file with a lot of [urlid=12345] (the 12345 can be any sequence of numbers, and it's not only 5 numbers, could be 3, 6, etc). I need to replace all those with the actual URL's that they reference, which I have in an excel file with two columns: urlid and url. The urlid column matches the urlid in [urlid=12345] in the txt file, and the url column contains the url I need to replace every [urlid=12345] with in the txt file.
0
Please suggest me regular expression to allow all decimal number or text such as "na" OR "NA"
like
4.4 correct
0.5 correct
6 correct
te WRONG
na correct
NA correct
0
I need help identifying a regular expression pattern to find the number of occurrences of a specific pattern within a given string.
 
For example, the pattern to be sought is "1" followed by 2 or more zeros and then followed by "1".
 
As such, "00010000011110001111000000100", would have 3 matches. I manage to do this successfully, with the help of pattern ""[1]0+0+[1]".
 
The more complicated one which I am not been able to handle is:
 
"000100001000001001000000101000000000" should be returning 4 matches. This is because a single 1 at position 9 will be used for pattern matching on either side.
 
In RegEx technical terms, I think,, this is called "Backreference constructs", which I am not been able to implement correctly.

I also need an exactly opposite pattern too, which is "0" followed by 2 or more ones and then followed by "0". However, I believe it can easily be done by just flipping numbers around from one correct answer.
0
How do I remove 0's from a number using regular expression?

For example: 12300456
Result: 123456
0
I am using Notepad++ to do a find and replace using regular expression.

I use the find to search the XML file for numbers within an Order element that is great than 8 digits by using the regular expression:

<Order>[^']{9,30}?</Order>

I need a regular expression for the replace option to trim down the number to the first 8 digits.

Example Find Result: <Order>123456780000000</Order>
Example Replace Result: <Order>12345678</Order>
0
Looking for a REGEX to extract the last field delimited by the pipe symbol(myContractNo) in this example:

FAS HC508C016P |116006|116006|myContractNo| 1  QR_CODE 2018-08-15T11:32:46-04:00
0
Bootstrap 4: Exploring New Features
LVL 12
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Hi Everyone,

I am trying to use regular expressions to parse the date from roughly 40 different file name strings in an automated environment. I have one solution but it’s not returning the correct string in all cases I need it to. I think it should be fairly straightforward, The problem is the names have different formats for the date piece and in some file names the numeric string is longer than 16 digits and highlights both parts as if they are two dates when they aren't. Also I have not come across any abbreviated year format's so checking for 20** should be acceptable. Finally I have never seen a file with a date pattern of mmddyyyy or any combination where year is at the end of the string, so this format type should not be considered.

My RegEx string I'm trying to build at this point.
"\d{4}\d{4}|\d{4}\d{1,2}|\d{4}-\d{1,2}-\d{1,2}"

Open in new window

I just don't have enough experience with more complex expressions.

Below are the main date types that I am trying to parse, unfortunately I don't have any control over the naming convention of the files themselves so I must be prepared for any and all of the following...

File_type_OneA_2018-8-09.csv <-Month and day sections are not always consistent
FileTypeOneB-2018-6-29.csv

File Type Two 201807.xls <-No day value

201310140703_FileTypeThreeA.csv  <-where the date is the first 8 chars.
20180531_FileTypeThreeB.csv

FileTypeFour-20180713090107228.xml <-I cannot say how the next 9 digits are …
0
What would be the JavaScript regex (or another approach) to detect in a string (path from a website)  that either starts with:

/small-business
/wealth-management
/commercial-banking

or contains:

campaigns
0
Hi team,

How to validate square bracket '[ and ]' in JavaScript (regular expression).

I have a textbox having value like this. So, I need a regular expression which will tell me square bracket is present in the textbox or not.

If there is a square bracket present in the textbox it will give me a error message.

[ABC  ESS080820183171][CTK  ESS080820189505][TROUS  ESS080820183485][SIMINESS  ESS080820184038][RAMMMM  ESS080820185998]
0
I have noticed a bot that is using several ip numbers at the same time that I want to block.  This will take a custom filter but all of the tutorials to create a filter take you up to the status code column.  I want the regex to catch HeadlessChrome.

The problem is that the browser name is much further past the status code as you can see below:


1.1.1.1 - - [05/Aug/2018:17:23:51 -0400] "POST /?wc-ajax=get_refreshed_fragments HTTP/1.1" 200 273 "https://www.theherbsplace.com/Fat_Grabbers_120_Capsules_p_189.html" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/64.0.3282.119 Safari/537.36"

Please tell me how to build a regex that goes past the status code and picks up the browser name.
0
var request = "{ "merchantRef":"ffd031516002"";

function camelToUnderscore(str) {

      return str.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase(); //merchant_ref

}

i would want to return merchant_ref matching string before ":
0
How do I find and replace to remove inside div child element, keeping everything (all <p>*</p>'s) .  some times their is just on p element, and sometimes alot.
link to regex101
<div class="expand_collapse section_box">
     <h2>Animal </h2>
     <div class="box_content">                        
          <div class="expand_collapse section_box">
              <h2>Agent Facing</h2>
               <div class="box_content">
                  <p><a href="/Brochure.pdf" target="_blank">Animal<br>(bully breeds &amp; bite history)</a></p>                                                   
              </div>
          </div>
          <div class="expand_collapse  section_box">
              <h2>Consumer Facing</h2>
              <div class="box_content">
                 <p>content</p>
              </div>
          </div>
       </div>
   </div>
   <!-- Next Product -->

Open in new window


I just want to remove:
 <div class="expand_collapse section_box">
      <h2>Agent Facing</h2>
       <div class="box_content">

Open in new window

and
                           </div>
                        </div>
                        <div class="expand_collapse  section_box">
                          <h2>Consumer Facing</h2>
                          <div class="box_content">
                            <p>content</p>
                          </div>
                        </div>

Open in new window

so keeping one or more of the 'P' elements inside the second div.box_content element.
I think I need to create a group for one or many p elements  (<p>.*</p>)  then replace with $1? So I keep the p's? (This does not work)
Can you provide a link to a resource to help me with regex as I do a lot of replaces/removals.

It would look like this when done.
<div class="expand_collapse section_box">
    <h2>Animal </h2>
     <div class="box_content">                        
         <p><a href="/Brochure.pdf" target="_blank">Animal<br>(bully breeds &amp; bite history)</a></p>
      </div>
</div>
<!-- Next Product -->

Open in new window


Thanks in advance.   I am not good with regex.
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.