RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find Match substrings of string seprated by spaces while keeping substring groups enclosed by certain character(s) together.
Expression
\S*?[\["].*?[\]"]|\S+
Description
Given that you have a string that is comprised characters/words separated by spaces and you want to creates matches of those words/characters but you also have substring groups of words/characters/spaces enclosed in [] and "" that you want treated as a single match, this pattern is what worked for me. Feel free to modify this regex to fit your needs if your string groups are not grouped by [] or "". Please let me know if this doesn't test well for you. ********** Example Test String: 212.183.143.4 - - [03/May/2007:21:03:27 +0100] "GET mp4-amrnb.3gp RTSP/1.0" 200 155527 [RealOnePlayer_s60.26.25.01_21-Jun-2005_13:02:43_epoc_av21_thumb] [3e546ed1-f9b1-11db-eb72-1d98b80ca9f5] [UNKNOWN] 454492 102 47 0 0 [0 0 0 0] [03/May/2007:21:02:38] 192.168.0.2 24245 55 ********** Matches: Matches(0) = "212.183.143.4" Matches(1) = "-" Matches(2) = "-" Matches(3) = "[03/May/2007:21:03:27 +0100]" Matches(4) = ""GET mp4-amrnb.3gp RTSP/1.0"" Matches(5) = "200" Matches(6) = "155527" Matches(7) = "[RealOnePlayer_s60.26.25.01_21-Jun-2005_13:02:43_epoc_av21_thumb]" Matches(8) = "[3e546ed1-f9b1-11db-eb72-1d98b80ca9f5]" Matches(9) = "[UNKNOWN]" Matches(10) = "454492" Matches(11) = "102" Matches(12) = "47" Matches(13) = "0" Matches(14) = "0" Matches(15) = "[0 0 0 0]" Matches(16) = "[03/May/2007:21:02:38]" Matches(17) = "192.168.0.2" Matches(18) = "24245" Matches(19) = "55" ********** ASP Test Code: Set regEx = New RegExp regEx.Global = True regEx.IgnoreCase = True regEx.MultiLine = True teststring = "<<your string>>" regEx.Pattern = "\S*?[\[|\""].*?[\]|\""]|\S+" Set Matches = regEx.Execute(teststring)
Matches
any [strings matched] "any at" all
Non-Matches
^\s*$
Author Rating: Not yet rated. Doug Drudik
Source
Your Rating
Bad Good

Enter New Comment

Title

Name

Comment

Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Copyright © 2001-2024, RegexAdvice.com | ASP.NET Tutorials