473,657 Members | 2,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex Help

All,

how do I describe a string consisting of any number of characters, with
an optional (but unique when occuring) end-of-line expression '/'? With
groups if possible, as in "(?<sentence>.* (?<eos>[^\\][\\]{1}$)?)"

Say I have the phrase 'My Taylor is /' as input; I'd like 'sentence'
and 'eos' to match.

Now if I have 'My Taylor is //' or 'My Taylor is / ', 'sentence' should
match, but not 'eos'. In other words, 'eos' should match only when it
occurs once, and last, in the string.

I have tried nesting eos within sentence, juxtaposing them, removing
quantifiers, removing the end-of-string assertion... nothing seems to
work satisfactorily.

Thanks for any help,
Jules

Aug 17 '06 #1
36 1781
Sorry, misspelling: please read "(?<sentence>.* (?<eos>[^\\][\\]{1}$)?)"
as "(?<sentence>.* (?<eos>[^/][/]{1}$)?)"

J.

Jules wrote:
All,

how do I describe a string consisting of any number of characters, with
an optional (but unique when occuring) end-of-line expression '/'? With
groups if possible, as in "(?<sentence>.* (?<eos>[^\\][\\]{1}$)?)"

Say I have the phrase 'My Taylor is /' as input; I'd like 'sentence'
and 'eos' to match.

Now if I have 'My Taylor is //' or 'My Taylor is / ', 'sentence' should
match, but not 'eos'. In other words, 'eos' should match only when it
occurs once, and last, in the string.

I have tried nesting eos within sentence, juxtaposing them, removing
quantifiers, removing the end-of-string assertion... nothing seems to
work satisfactorily.

Thanks for any help,
Jules
Aug 18 '06 #2
Jules wrote:
how do I describe a string consisting of any number of characters, with
an optional (but unique when occuring) end-of-line expression '/'? With
groups if possible, as in "(?<sentence>.* (?<eos>[^\\][\\]{1}$)?)"

Say I have the phrase 'My Taylor is /' as input; I'd like 'sentence'
and 'eos' to match.

Now if I have 'My Taylor is //' or 'My Taylor is / ', 'sentence' should
match, but not 'eos'. In other words, 'eos' should match only when it
occurs once, and last, in the string.
(?<sentence.* [^/] ) (?<eos/ ) $

RegexOptions.Ig norePattermWhit espace

The sentence group may not end with a "/", and the eos group must be
the last character.

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.
Aug 18 '06 #3
Hi Jules,

Check out how you can use "?" as a lazy modifier to existing quantifiers:

http://msdn.microsoft.com/library/de...uantifiers.asp

And negative lookbehind assertion:

http://msdn.microsoft.com/library/de...constructs.asp

Try this with the IgnorePatternWh itespace and Singleline options:

string pattern =
@"(?<sentence>. *? ) # lazily match sentence so that '.' does not capture eos
(?<eos(?<! /) # negative lookbehind for /
/)?$ # match single '/' at end of string or just end of string";

--
Dave Sexton

"Jules" <er************ ***@gmail.comwr ote in message news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
Sorry, misspelling: please read "(?<sentence>.* (?<eos>[^\\][\\]{1}$)?)"
as "(?<sentence>.* (?<eos>[^/][/]{1}$)?)"

J.

Jules wrote:
>All,

how do I describe a string consisting of any number of characters, with
an optional (but unique when occuring) end-of-line expression '/'? With
groups if possible, as in "(?<sentence>.* (?<eos>[^\\][\\]{1}$)?)"

Say I have the phrase 'My Taylor is /' as input; I'd like 'sentence'
and 'eos' to match.

Now if I have 'My Taylor is //' or 'My Taylor is / ', 'sentence' should
match, but not 'eos'. In other words, 'eos' should match only when it
occurs once, and last, in the string.

I have tried nesting eos within sentence, juxtaposing them, removing
quantifiers, removing the end-of-string assertion... nothing seems to
work satisfactorily.

Thanks for any help,
Jules

Aug 18 '06 #4
Dave Sexton wrote:
string pattern =
@"(?<sentence>. *? ) # lazily match sentence so that '.' does not capture eos
(?<eos(?<! /) # negative lookbehind for /
/)?$ # match single '/' at end of string or just end of string";
Almost - this matches "My Taylor is /" twice: once as it should, and
again on the empty string after the /. Those optional matches are
tricky!

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.
Aug 18 '06 #5
Hi John - I'm not sure what you mean by matching the empty string after
'/'

Jon Shemitz wrote:
Dave Sexton wrote:
string pattern =
@"(?<sentence>. *? ) # lazily match sentence so that '.' does not capture eos
(?<eos(?<! /) # negative lookbehind for /
/)?$ # match single '/' at end of string or just end of string";

Almost - this matches "My Taylor is /" twice: once as it should, and
again on the empty string after the /. Those optional matches are
tricky!

--

.NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.
Aug 18 '06 #6
Jules wrote:
Hi John -
Who?
I'm not sure what you mean by matching the empty string after '/'
The Matches method returns a MatchCollection with a Count of 2.
(Alternatively, Regex.Match().N extMatch.Succes s == true.)

The 2nd Match has a Length of 0, and a Value of "".

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.
Aug 19 '06 #7
Jules wrote:
Hi John - I'm not sure what you mean by matching the empty string after
'/'
A regular expression which is composed entirely of elements which can
match zero-width strings (foo*, foo?, foo{0,<whatever >}) itself matches
each zero-width string (i.e. the gap between each character). The
difference with this regular expression is that it contains an assertion
at the end - the '$' - that's what stops it finding every zero-length
string between each character in the source. Instead, it finds only the
last one (as well as the actual match).

-- Barry

--
http://barrkel.blogspot.com/
Aug 19 '06 #8
Hi,

I'm not sure where the problem is.

I tested the expression and it worked according to the op's requirements. Is it returning extra information that the op doesn't
require? Does that matter? Is there a better way?

--
Dave Sexton

"Barry Kelly" <ba***********@ gmail.comwrote in message news:l5******** *************** *********@4ax.c om...
Jules wrote:
>Hi John - I'm not sure what you mean by matching the empty string after
'/'

A regular expression which is composed entirely of elements which can
match zero-width strings (foo*, foo?, foo{0,<whatever >}) itself matches
each zero-width string (i.e. the gap between each character). The
difference with this regular expression is that it contains an assertion
at the end - the '$' - that's what stops it finding every zero-length
string between each character in the source. Instead, it finds only the
last one (as well as the actual match).

-- Barry

--
http://barrkel.blogspot.com/

Aug 19 '06 #9
I ran the following console app:

static void Main(string[] args)
{
RegexOptions options = RegexOptions.Ig norePatternWhit espace | RegexOptions.Si ngleline;

string pattern =
@"(?<sentence>. *? ) # lazily match sentence so that '.' does not capture eos
(?<eos(?<! /) # negative lookbehind for /
/)?$ # match single '/' at end of string or just end of string";

// Expected: implicit, outer group match, "My Taylor is " and "/" (3)
Match("My Taylor is /", pattern, options, 3);

// Expected: implicit, outer group match and "My Taylor is //" (2)
Match("My Taylor is //", pattern, options, 2);

// Expected: implicit, outer group match and "My Taylor is / " (2)
Match("My Taylor is / ", pattern, options, 2);

Console.ReadLin e();
}

private static void Match(string input, string pattern, RegexOptions options, int expectedSuccess Count)
{
Match match = Regex.Match(inp ut, pattern, options);

Console.WriteLi ne("Input={0}: {1}", input, (match.Success) ? "Success" : "Failed");
Console.WriteLi ne("Expected # of successful groups: " + expectedSuccess Count);
Console.WriteLi ne();

if (!match.Success )
return;

Console.WriteLi ne("Match: {0}", match);
Console.WriteLi ne();

Console.WriteLi ne("Captures: ");

for (int c = 0; c < match.Captures. Count; c++)
Console.WriteLi ne("{0}: {1}", c, match.Captures[c]);

Console.WriteLi ne();

Console.WriteLi ne("Groups: ");

int actualSuccessCo unt = 0;

for (int g = 0; g < match.Groups.Co unt; g++)
{
if (match.Groups[g].Success)
actualSuccessCo unt++;

Console.WriteLi ne("{0} ({1}): {2}", g, (match.Groups[g].Success) ? "Success" : "Failed", match.Groups[g]);
}

Console.WriteLi ne();
Console.WriteLi ne("Success: {0}", (expectedSucces sCount == actualSuccessCo unt) ? "Yes" : "No");

Console.WriteLi ne(new string('-', 10));
Console.WriteLi ne();
}

which produced the following output:

Input=My Taylor is /: Success
Expected # of successful groups: 3

Match: My Taylor is /

Captures:
0: My Taylor is /

Groups:
0 (Success): My Taylor is /
1 (Success): My Taylor is
2 (Success): /

Success: Yes
----------

Input=My Taylor is //: Success
Expected # of successful groups: 2

Match: My Taylor is //

Captures:
0: My Taylor is //

Groups:
0 (Success): My Taylor is //
1 (Success): My Taylor is //
2 (Failed):

Success: Yes
----------

Input=My Taylor is / : Success
Expected # of successful groups: 2

Match: My Taylor is /

Captures:
0: My Taylor is /

Groups:
0 (Success): My Taylor is /
1 (Success): My Taylor is /
2 (Failed):

Success: Yes
----------

I think this meets the requirements set by Jules' in the OP.
Jules, can you please comment to the effectiveness of the expression according to your requirements?

--
Dave Sexton

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message news:%2******** **********@TK2M SFTNGP06.phx.gb l...
Hi,

I'm not sure where the problem is.

I tested the expression and it worked according to the op's requirements. Is it returning extra information that the op doesn't
require? Does that matter? Is there a better way?

--
Dave Sexton

"Barry Kelly" <ba***********@ gmail.comwrote in message news:l5******** *************** *********@4ax.c om...
>Jules wrote:
>>Hi John - I'm not sure what you mean by matching the empty string after
'/'

A regular expression which is composed entirely of elements which can
match zero-width strings (foo*, foo?, foo{0,<whatever >}) itself matches
each zero-width string (i.e. the gap between each character). The
difference with this regular expression is that it contains an assertion
at the end - the '$' - that's what stops it finding every zero-length
string between each character in the source. Instead, it finds only the
last one (as well as the actual match).

-- Barry

--
http://barrkel.blogspot.com/


Aug 19 '06 #10

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

Similar topics

6
4789
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search string. It's actually the input string into a Microsoft Index Server search. The string will consist of words, perhaps enclosed in quotes or parentheses. I'd like to use Regex to pull out the words, or the phrases if the words are enclosed in quotes....
20
8092
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex matches are called within a loop (like if or for). E.g. for(int i = 0; i < 10; i++) { Regex r = new Regex();
17
3961
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 http://forta.com/books/0672325667/
7
2226
by: Mike Labosh | last post by:
I have the following System.Text.RegularExpressions.Regex that is supposed to remove this predefined list of garbage characters from contact names that come in on import files : Dim _dropContactGarbage As New Regex( _ "(+)|" & _ "(+)|" & _ "(+)|" & _ "(+)|" & _ "(+)|" & _
9
2079
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat lot of times here.... Requirement: ------------------- I want to get the value of "href" i.e "city1.html" by searching "city1" between the <a</atag. Please note that "city1" can repeat lot of
4
2633
by: Chris | last post by:
Hi Everyone, I am using a regex to check for a string. When all the file contains is my test string the regex returns a match, but when I embed the test string in the middle of a text file a match is never returned. The string that I give to the regex is one that contains the entire contents of a text file. I'm using the multi-line option and I've also tried stripping out the VbCr
7
2580
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward slash then some numbers. For some reason I am not getting that. It won't work at all in 2.0
6
4202
by: Phil Barber | last post by:
I am using Regex to validate a file name. I have everything I need except I would like the dot(.) in the filename only to appear once. My question is it possible to allow one instance of character but not two or more? example myfile.doc = good My.file.doc = not good if you could give an example of the expression pattern that would most helpful. thanks phil
1
12176
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose is that I seek out terms which are in a glossary on our site, and automatically link to this definition. Its slightly complex becase certain elements have to be ignored, for exampleI dont want to add links within existing links, or for example link...
0
1622
by: Support Desk | last post by:
That’s it exactly..thx -----Original Message----- From: Reedick, Andrew Sent: Tuesday, June 03, 2008 9:26 AM To: Support Desk Subject: RE: regex help The regex will now skip anything with an '@'in the filename on the assumption it's already in the correct format. Uncomment the os.rename line
0
8411
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6176
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.