473,418 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

regex: multiple matches after initial text

I'm trying to match a set of matches after some initial text:

mytext: "something" "somethingelse" "another thing"
"maybe another"
(?:mytext: )(?<mymatch>["]{1,1}[^"]+["]{1,1}[\s| ]+)+

I only get the last one "maybe another". I want to get all the values with
quotes as a group, hence the + after the ()'s.

Is this possible?
Nov 17 '05 #1
10 3067
Chance Hopkins wrote:
I'm trying to match a set of matches after some initial text:

mytext: "something" "somethingelse" "another thing"
"maybe another"
(?:mytext: )(?<mymatch>["]{1,1}[^"]+["]{1,1}[\s| ]+)+

I only get the last one "maybe another". I want to get all the values with
quotes as a group, hence the + after the ()'s.

Is this possible?

Regex regex = new Regex(@"
((?<mymatch>(?<="")\w+\s*\w+(?="")|[^\s""]+))",
RegexOptions.ExplicitCapture);

Test input:
"something" "somethingelse" "another thing" "maybe another"

Test output:
mymatch =»something«=
mymatch =»somethingelse«=
mymatch =»another thing«=
mymatch =»maybe another«=

--
Take care,
Ken
(to reply directly, remove the cool car. <sigh>)
Nov 17 '05 #2

"Ken Arway" <ka****@jaguar.att.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Chance Hopkins wrote:
I'm trying to match a set of matches after some initial text:

mytext: "something" "somethingelse" "another thing"
"maybe another"
(?:mytext: )(?<mymatch>["]{1,1}[^"]+["]{1,1}[\s| ]+)+

I only get the last one "maybe another". I want to get all the values
with quotes as a group, hence the + after the ()'s.

Is this possible?

Regex regex = new Regex(@" ((?<mymatch>(?<="")\w+\s*\w+(?="")|[^\s""]+))",
RegexOptions.ExplicitCapture);

Test input:
"something" "somethingelse" "another thing" "maybe another"

Test output:
mymatch =»something«=
mymatch =»somethingelse«=
mymatch =»another thing«=
mymatch =»maybe another«=

--
Take care,
Ken
(to reply directly, remove the cool car. <sigh>)


Thanks for the help.

Is it possible to target this match after an initial match (so that I only
match ""'s after the text "mytext: "), without splitting the string first?

I'm sure I could use SubString with IndexOf "mytext: ", but I'd prefer to do
this all with RegEx if possible.

I'm working on a PPC with a 300mhz processor and have to do a large number
of these inside a case statement to begin with. It's got a thread running a
loop and I really need to try and be as minimal as possible.

Thanks again.

Nov 17 '05 #3
Hello Chance,
mytext: "something" "somethingelse" "another thing"
"maybe another"


maybe you want it a more generic way :
Regex regex = new Regex(@"(?<="")(\s*\w+\s*)+(?="")");
foreach (Match match in regex.Matches(inputText.Text))
outputText.AppendText(match.Value+"\r\n");

so you get also the words "another third token" etc.
ciao Frank
--
Dipl.Inf. Frank Dzaebel [MCP C#]
http://Dzaebel.NET
Nov 17 '05 #4

"Frank Dzaebel" <Po**@FranksSeite.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hello Chance,
mytext: "something" "somethingelse" "another thing"
"maybe another"


maybe you want it a more generic way :
Regex regex = new Regex(@"(?<="")(\s*\w+\s*)+(?="")");
foreach (Match match in regex.Matches(inputText.Text))
outputText.AppendText(match.Value+"\r\n");

so you get also the words "another third token" etc.
ciao Frank
--
Dipl.Inf. Frank Dzaebel [MCP C#]
http://Dzaebel.NET


Hi Frank, thanks for the help. I don't think I was clear in my first post.

Is it possible to target this match after an initial match (so that I only
match ""'s after the text "mytext: "), without splitting the string first?

I have other text in the document, prior to this text. I'm confused on
whether or not I can put those repeating patterns into a group object. I
only seem to get (using my expression) one match with two groups. Group 0 is
the whole string and groups 1 is the last "" pair with text.

I'm sure I could use SubString with IndexOf "mytext: ", but I'd prefer to do
this all with RegEx if possible.

*see other reply for "why" if interested.
Nov 17 '05 #5
Chance Hopkins wrote:
Is it possible to target this match after an initial match (so that I only
match ""'s after the text "mytext: "), without splitting the string first?


I think that's different from what I understood from your first post.
To clarify, please provide a test input string and the output you want.

--
Take care,
Ken
(to reply directly, remove the cool car. <sigh>)
Nov 17 '05 #6
In article <us**************@TK2MSFTNGP10.phx.gbl>,
Chance Hopkins <ch************@hotmail.com> wrote:

: Is it possible to target this match after an initial match (so that I
: only match ""'s after the text "mytext: "), without splitting the
: string first?
: [...]

Use something similar to the following:

string input =
"mytext: \"something\" \"somethingelse\" " +
"\"another thing\" \"maybe another\"";

string pat = @"^mytext:(\s*'(?<strings>[^\\']*(\\.[^\\']*)*)')+\s*$";
Regex regex = new Regex(pat.Replace('\'', '"'));

Match m = regex.Match(input);
if (m.Success)
foreach (Capture c in m.Groups["strings"].Captures)
Console.WriteLine(c);

The regular expression for catching the individual strings is based
on code from Damian Conway's Text::Balanced module:

http://search.cpan.org/~dconway/Text...xt/Balanced.pm

Hope this helps,
Greg
Nov 17 '05 #7
Sorry for the delay in responding, my week is a bit off from everyone else.
You're right, I did a real poor job of explaining this. Let me try again.
I'm trying to pull multiple matches after an initial match, for instance:
onevalue: "asdf" "1234"
twovalue: "9999" "8888"
"7777" "6666"
threevalue: "4444" 3333"
"1111"
with something like this:

(?:threevalue: )(?<mymatch>["]{1,1}[^"]+["]{1,1}[\s| ]+)+
I want to do a non capturing match (?:threevalue: ) and then do a group
capture after that of all values inside the "'s. Right now, I just get the
final one "1111". If I remove my initial match in the expression I match all
quoted values in the entire document and can retrieve them with a group
object.

I'm aware I can fix this by doing a substring and then a regex on that, but
I'm on a PPC and trying to save every bit of processor. I have to do this 16
or so times inside a loop that fires sometimes 1000 times or more. I'd
really like to do it all in Regex to save on all the substring calls, but I
don't even know if it's possible.
Thanks for the feedback.

"Ken Arway" <ka****@jaguar.att.net> wrote in message
news:uj*************@TK2MSFTNGP15.phx.gbl...
Chance Hopkins wrote:
Is it possible to target this match after an initial match (so that I
only match ""'s after the text "mytext: "), without splitting the string
first?


I think that's different from what I understood from your first post. To
clarify, please provide a test input string and the output you want.

--
Take care,
Ken
(to reply directly, remove the cool car. <sigh>)

Nov 17 '05 #8
In article <Oz**************@TK2MSFTNGP10.phx.gbl>,
Chance Hopkins <ch************@hotmail.com> wrote:

: [...]
: I'm aware I can fix this by doing a substring and then a regex on
: that, but I'm on a PPC and trying to save every bit of processor. I
: have to do this 16 or so times inside a loop that fires sometimes 1000
: times or more. I'd really like to do it all in Regex to save on all
: the substring calls, but I don't even know if it's possible.

Did you see my followup[*]?
[*] Message-ID: <11*************@corp.supernews.com>

Perhaps the more important question is whether you're engaging in
premature optimization.

Greg
Nov 17 '05 #9

"Greg Bacon" <gb****@hiwaay.net> wrote in message
news:11*************@corp.supernews.com...
In article <Oz**************@TK2MSFTNGP10.phx.gbl>,
Chance Hopkins <ch************@hotmail.com> wrote:

: [...]
: I'm aware I can fix this by doing a substring and then a regex on
: that, but I'm on a PPC and trying to save every bit of processor. I
: have to do this 16 or so times inside a loop that fires sometimes 1000
: times or more. I'd really like to do it all in Regex to save on all
: the substring calls, but I don't even know if it's possible.

Did you see my followup[*]?

[*] Message-ID: <11*************@corp.supernews.com>

Actually I didn't (for some reason maybe it didn't propigate over to the MS
server, maybe) and THANK YOU for speaking up. That got it going.

It was the captures group I had to loop through. Everything is WONDERFUL now
:-)

Thanks to you other guys for the help also.


Perhaps the more important question is whether you're engaging in
premature optimization.

Greg

Nov 17 '05 #10
In article <u#**************@tk2msftngp13.phx.gbl>,
Chance Hopkins <ch************@hotmail.com> wrote:

: Actually I didn't (for some reason maybe it didn't propigate over
: to the MS server, maybe) and THANK YOU for speaking up. That got
: it going.

Good deal. Glad to help.

Greg
Nov 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
2
by: John Baro | last post by:
I need to determine when multiple fonts are selected in a richtextbox. A font is indicated by \fcharset(N) where (N) is a number. (To the best of my knowledge) I can use this statement int...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
5
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated...
4
by: shonend | last post by:
I am trying to extract the pattern like this : "SUB: some text LOT: one-word" Described, "SUB" and "LOT" are key words; I want those words, everything in between and one word following the...
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
11
by: proctor | last post by:
hello, i have a regex: rx_test = re.compile('/x()*x/') which is part of this test program: ============ import re
7
by: Nightcrawler | last post by:
Hi all, I am trying to use regular expressions to parse out mp3 titles into three different groups (artist, title and remix). I currently have three ways to name a mp3 file: Artist - Title ...
2
by: O.B. | last post by:
In the following example, the Matches operation never returns 4 matches as I am expecting. What's wrong with my syntax? private const string DOUBLE_REGEX = @"?*?*"; private const string...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.