Regular Expressions

6K

Solutions

7

Articles & Videos

5K

Contributors

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

is it possible to set up a regex to validate each position in a string of characters in a form field?

I have a requirement for a contract number field in a Cold Fusion form. Requirements are:

•      Positions 1-6 will be the  70Z0XX where XX is the contracting office code IE: 23.
•      Positions 7-8 will be the two digit Fiscal Year IE: 17.
•      Position 9 will be the one character instrument code IE: C, D, F.
•      Positions 10-17 will be agency assigned number.
•      New Example: 70Z02317D00000001

Is it possible to set up an expression at different points w/in the string of characters? I've never done that...only length requirements and forcing it to start w/ either a number or a letter. thanks for any help.
0
Enroll in June's Course of the Month
LVL 8
Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Hi,

I am in need of some assistance with a character search in any string for the followign characers

-
[
]
*
!

Any and all help would be very much appreciated.

Thanks

Simon
0
I am trying to gather all of the characters following the final space in a string but am struggling with the syntax. Can anyone help please? The last set of characters will always be alphanumeric but will be of differing characters and amount of characters.

 In the below example I would only want to highlight cat.  

 dog xxx-klsfkd-sdf-sdf cat


 And in the next one aaaaaaaaaaaaaddddddddddddddd

 heht ooijl-nanhjhsh aaaaaaaaaaaaaddddddddddddddd


however if the final character in the string is single I wish to ignore it e.g.

 heht ooijl-nanhjhsh aaaaaaaaaaaaaddddddddddddddd 0

would still highlight aaaaaaaaaaaaaddddddddddddddd


 Many Thanks
0
I am trying to gather all of the characters following the final space in a string but am struggling with the syntax. Can anyone help please? The last set of characters will always be alphanumeric but will be of differing characters and amount of charaters.

In the below example I would only want to highlight cat.  

dog xxx-klsfkd-sdf-sdf cat


And in the next one aaaaaaaaaaaaaddddddddddddddd

heht ooijl-nanhjhsh aaaaaaaaaaaaaddddddddddddddd


Many Thanks
0
Hi All,

Background:
I have written a script to grab the most common words in a page (with tags stripped etc.) It mostly works, however there is an occasional occurrence of the following happening: helloThisIsAnExampleOfTheAnomoly.

This occurs while grabbing certain HTML via a cURL based function, stripping tags and counting word frequency. It mostly appears to occur in menus and widgets.

What I'm looking for is an elegant/efficient solution to pop/push/unset values in the array with the values split.

To expand:
preg_replace('/(?<! )(?<!^)[A-Z]/',' $0', $words)

Open in new window


I'm using the above regular expressions to essentially split the values based on uppercase values occurring mid string/array element.

To summarise:
$array is currently something like this: ("This", "is", "okay", "this", "IsNotOkay")
What I want:
$array is going to look something like this ("This", "is", "okay", "this", "Is", "Not", "Okay")

Don't worry too much about the repeat values as I am utilising a "stop words" array to rid the ones I would not like to keep.

I've not got it working nicely yet so thought I'd turn to you for your expert input.

Thanks in advance.
Chris
0
I am looking to replace the value between two spaces in a string with a backslash. Can someone assist with the syntax.

E.G. I want

RED 123456789 White

to become

RED\White

Thanks
0
I am looking for a solution that uses CFSCRIPT to remove all text that is surrounded by '<style' and '/style>' including removing the style tags. Can someone show me how I can use something like ReReplaceNoCase to solve this.

For example: if I have text that looks like:

section a <style>here is style to remove</style>section b <style>more style to remove</style>section c

I need the function to return:
section a section b section c

Thank you.
0
I have a regular expression that does exactly what I want it to do but I don’t understand how it works.  I'm running a bash shell under MAC OS Sierra.

Here’s the command: sed -e 's/.*\"\(.*\)\"/\1/'

Here’s what it’s applied to: | "IOPlatformSerialNumber" = "QP93505K0TM"

which returns this: QP93505K0TM

which is exactly what I want.

The pattern match has two parts:

s/.*\”   and (.*\)\”

If I run just part one, it returns the entire string, which I expect because I read the first part to mean: “Match any string than ends with a quote”.

Part two always grabs the last part of the string.  I tested this:

“abc” “def” returns def

“abc” “def” “ghi” returns ghi

“abc” “def” “ghi” “jol” returns jul

My question is why does the expression enclosed in () always refer to the last part of the string?

Thanks.
0
I had this question after viewing adding leading zeros and trailing zeros.

I have the following:

C  --------------                                                                                                                                                                                    
P  IMAGE PANEL                                                                                                                                                                                        
C  --------------                                                                                                                                                                                    
C                                                                                                                                                                                                    
C                                                                                                                                                                                                    
C  ALIGN BOARD RECORDS :   # I need to match this pattern                                                                                                                                                                          
C                                                           …
0
Can I use ReReplaceNoCase or someother javascript function to  remove everything in a string from and including '<style' to '/style>'? What would the function call look like?
0
Technology Partners: We Want Your Opinion!
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Hi

I'm trying to create a template  using a REGEX rule to discard the last twenty four characters (" action=allow cache=MISS") of the following string when RSYSLOG receives notifications that contain the undesired sequence:
192.168.22.167 GET http://clients3.google.com/generate_204 action=allow cache=MISS

Open in new window

I can select the sequence using /( action=allow cache=MISS)/g but I can't find the solution to invert the selection in order to retain only the first part of the notification message
any ideas ?
thanks
yann
0
Hi I have a regex which works fine on

ABCDE123456X  - /LYBLA[0-9]{6}X/i (Fixed ABCDE, followed by 6 digits, and a fixed X.

I need to change this to be able to use a 7th digit instead of the final fixed X as well as the original format, for example

ABCDE1234567 and ABCDE987654X

Please advise
0
I have a form w/ several fields...one of the fields requirements states: The Contract Number is required and can contain no more than 22 alphanumeric characters. This can't contain spaces however:

here's the current regex setup:

	, Contract_Number: {
				required: true
			   ,pattern: /^[DdFfGgHhNnVv][A-Za-z0-9]*$/

Open in new window


Does anyone have any suggestions on how to modify this?
0
I need to pull two values from a repeating string. I've got it working 99% but it's ugly.

Looking for a trick here if you see one.

/******************************************
I got a simple solution, see comments
leaving here for any tips you might have
******************************************/

I am using REGEXP_COUNT to determine I have repeating strings, but that doesn't help me with the string manipulation.

Here's the data, it's a varchar2(600),
ATTENDED ECOLE NAVALE/FRANCE AS AN EXCHANGE STUDENT, SPRING '09
RECEIVED A LANGUAGE STUDY MINOR IN FRENCH
RECEIVED A LANGUAGE STUDY MINOR IN SPANISH

RECEIVED A LANGUAGE STUDY MINOR IN JAPANESE
RECEIVED A LANGUAGE STUDY MINOR IN CHINESE

ATTENDED NOVAMOVA, KIEV, FALL 2014 AS AN EXCHANGE STUDENT.
RECEIVED A LANGUAGE STUDY MINOR IN ARABIC. RECEIVED A LANGUAGE STUDY MINOR IN RUSSIAN.

RECEIVED A LANGUAGE STUDY MINOR IN RUSSIAN
RECEIVED A LANGUAGE STUDY MINOR IN SPANISH

RECEIVED A LANGUAGE STUDY MINOR IN ARABIC
RECEIVED A LANGUAGE STUDY MINOR IN SPANISH

For the first values, I get
FRENCH R
JAPANESE
ARABIC. R
RUSSIAN
ARABIC R

Here's my code -
The "9" is the length for the first value, to get the proper length would make this even uglier
The "+8" just points me in position to find the second value

Just showing how ugly it is. The only issue is getting the firs value cleanly.

     
substr(tn.notes, 
              -- find the position where the first minor starts
              

Open in new window

0
I want to match a url  such that it allows "abc.def.com" but not allow the following:
"abc.def.com/123"
"abc.def.com/456/789"

Thanks
0
Hi I need to parse the content between the first and second '-' in a string, ie  012-09324-5418567  (Oracle 10g)
The following does work for me; however, I was also wondering if there is a more efficient way to do this or how to do it using regexp_instr:
 
SELECT SUBSTR('012-09324-541',INSTR('012-09324-541', '-')+1, INSTR('012-09324-541', '-' ,1,2)-INSTR('012-09324-541', '-')-1) FROM DUAL;

Open in new window


Result:  09324
 
Thank you in advance for suggestions.
0
my regular expressions isnt' working...need one  for a POC form field that can include alphabetic characters [commas] [periods] and [spaces]

current regex is:
		, Contact_POC: {
				required: true
			  , pattern: /^[a-zA-Z0-9,-.!? ]*$/
			}

Open in new window


throwing an invalid message though when John Doe Test2 is used...anyone have any ideas?
0
I need a regex for an invoice amount field to match N.NN format but allow commas:

i.e.: 5,000.00 would be accepted as well as 5000.00 but not 5 or 50 or .50
0
Here's the code I'm using:

				jQuery.validator.addMethod(
		"money",
			function(value, element) {
				var isValidMoney = /^(\d+|\d{1,3}(,\d{3})*)(\.\d{2})?$/.test(value);
				if(isValidMoney) {
					var testValue = value;
					testValue = testValue.replace(",","",-1);
					if(testValue <= 0){
						isValidMoney = false;
					}
				}
				return this.optional(element) || isValidMoney;
			},
			"Insert"
		);

Open in new window


			}
			, Invoice_Amount: {
				  required: true
				, money: true
				, pattern: /^\d+\.\d{0,2}$/
				, min: .01
			}

Open in new window


Won't catch where I need it to. Anyone see where this would be failing?
0
Free Tool: Path Explorer
LVL 8
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

I need a regex to allow letters, hyphens and spaces
0
Server A - IIS configuration: 5 sites. Rewrite rule configured at the server level in IIS for one of the sites (as opposed to being set at the "sites" level). Rewrite rule is working properly here.

Server B - IIS configuration: 5 sites. Rewrite rule configured at the server level in IIS for one of the sites (as opposed to being set at the "sites" level). Rewrite rule is not working here.

The rewrite rule was copied from verbatim from Server A to Server B. Server B is supposed to be a clone of Server A, but it was built from scratch and then had IIS installed fresh (as opposed to being created via a clone function through the hypervisor).

Rewrite rule settings:

In the "Match URL" section
Requested URL: Matches the Pattern
Using: Regular Expressions
Pattern: (.*)
Ignore case checkbox: Checked

In the "Conditions" section
Logical grouping: Match All
Condition input: {CACHE_URL}
Check if input string: Matched the pattern
Pattern: ^(https?:\/\/)?[\w\.-]*(dataviewer)+
Ignore case checkbox: Checked

In the "Action" section
Action type: Rewrite
Action properties: Rewrite URL: http://localhost:8500/{R:0}
Append query string checkbox: Checked
Stop processing of subsequent rules checkbox: Checked

The way it was explained to me, anything that has the word "dataviewer" should match the pattern and therefore get redirected/rewritten. We have a test.html file in the root of the directory to perform testing. When testing the rule by going to the URL/Host …
0
Hi,

  I have a huge text file which i opened in Notepad++.
I want to find the words starting with SP_ and ending in a space.
Then i would like these words to be all listed into a column.

how can i achieve it?
Please help.
0
I am using the following to accept only 0 or 10 digits, but I need to change it to allow 0, 10 or 13. Can someone assist?

 if(/^(\d{0}|\d{10})$/.test(carCount) == false) {
......

Open in new window

0
regex code ( i use windows 7)  to remove .cn and .com  , thank you

john
mary
site.cn
mario.com
sileson
0
I would like to know how to write a regex which does not allow strings ending with dev, qa, prd, staging and the string should be only lowercase.

for example

abcqa not allowed
xyzdev not allowed
pqr allowed
abcdef - allowed
mnxpkdev - not allowed
lmnopstaging - not allowed
0

Regular Expressions

6K

Solutions

7

Articles & Videos

5K

Contributors

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.