473,749 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple Regex

Hey all,

Decided to give a shot at Regular expressions - need a bit of help :)

I can't seem to find the right regex for matching words like "*test*" or
*somevalue*" - in short, all words starting and ending with a *.

I tried things like string regex = @"(^*\|*^)") ;
but it still doesn't work completely (matches on "*test" too for example).
If anything could help me with this, that would be appreciated.

Also, does anyone have really GOOD regex tutorials? I read this one
http://www.regular-expressions.info/tutorial.html but that one is (imho)
still pretty hard to grasp.

Thanks!

Razzie
Nov 16 '05 #1
7 1912
"Razzie" <ra****@quickne t.nl> wrote in
news:Os******** *****@TK2MSFTNG P11.phx.gbl...
Hey all,

Decided to give a shot at Regular expressions - need a bit of help :)

I can't seem to find the right regex for matching words like "*test*" or
*somevalue*" - in short, all words starting and ending with a *.

I tried things like string regex = @"(^*\|*^)") ;
but it still doesn't work completely (matches on "*test" too for
example). If anything could help me with this, that would be appreciated.
You need to escape the asterix '*', because it's a regex metacharacter.
This pattern should work: @"\*\w+\*" (Read: Literally '*', followed by at
least one alphanumeric character, followed by another '*')
I've applied it to this post, and it only matched on the two samples above
Also, does anyone have really GOOD regex tutorials? I read this one
http://www.regular-expressions.info/tutorial.html but that one is (imho)
still pretty hard to grasp.


Using Expresso (www.ultrapico.com) usually makes things a lot easier.
If you want a really good text on it, get J. Friedl's "Mastering Regular
Expressions".

Niki
Nov 16 '05 #2
Thanks Niki, that works! And looks better too :P

I'll have a look at Expresso too.

Thanks again!

"Niki Estner" <ni*********@cu be.net> wrote in message
news:eA******** ******@TK2MSFTN GP15.phx.gbl...
"Razzie" <ra****@quickne t.nl> wrote in
news:Os******** *****@TK2MSFTNG P11.phx.gbl...
Hey all,

Decided to give a shot at Regular expressions - need a bit of help :)

I can't seem to find the right regex for matching words like "*test*" or
*somevalue*" - in short, all words starting and ending with a *.

I tried things like string regex = @"(^*\|*^)") ;
but it still doesn't work completely (matches on "*test" too for
example). If anything could help me with this, that would be appreciated.


You need to escape the asterix '*', because it's a regex metacharacter.
This pattern should work: @"\*\w+\*" (Read: Literally '*', followed by at
least one alphanumeric character, followed by another '*')
I've applied it to this post, and it only matched on the two samples above
Also, does anyone have really GOOD regex tutorials? I read this one
http://www.regular-expressions.info/tutorial.html but that one is (imho)
still pretty hard to grasp.


Using Expresso (www.ultrapico.com) usually makes things a lot easier.
If you want a really good text on it, get J. Friedl's "Mastering Regular
Expressions".

Niki

Nov 16 '05 #3
Hmm it's not 100% correct - it will match "*test*a" too (if there still is a
char after the last *.

Any ideas? (*^ doesn't seem to work either)

"Razzie" <ra****@quickne t.nl> wrote in message
news:eL******** *****@tk2msftng p13.phx.gbl...
Thanks Niki, that works! And looks better too :P

I'll have a look at Expresso too.

Thanks again!

"Niki Estner" <ni*********@cu be.net> wrote in message
news:eA******** ******@TK2MSFTN GP15.phx.gbl...
"Razzie" <ra****@quickne t.nl> wrote in
news:Os******** *****@TK2MSFTNG P11.phx.gbl...
Hey all,

Decided to give a shot at Regular expressions - need a bit of help :)

I can't seem to find the right regex for matching words like "*test*" or
*somevalue*" - in short, all words starting and ending with a *.

I tried things like string regex = @"(^*\|*^)") ;
but it still doesn't work completely (matches on "*test" too for
example). If anything could help me with this, that would be
appreciated.


You need to escape the asterix '*', because it's a regex metacharacter.
This pattern should work: @"\*\w+\*" (Read: Literally '*', followed by at
least one alphanumeric character, followed by another '*')
I've applied it to this post, and it only matched on the two samples
above
Also, does anyone have really GOOD regex tutorials? I read this one
http://www.regular-expressions.info/tutorial.html but that one is (imho)
still pretty hard to grasp.


Using Expresso (www.ultrapico.com) usually makes things a lot easier.
If you want a really good text on it, get J. Friedl's "Mastering Regular
Expressions".

Niki


Nov 16 '05 #4
"Razzie" <ra****@quickne t.nl> wrote in
news:un******** ******@TK2MSFTN GP09.phx.gbl...
Hmm it's not 100% correct - it will match "*test*a" too (if there still is
a char after the last *.

Any ideas? (*^ doesn't seem to work either)
'^' will only match on the start of the string/line.
What kind of characters are allowed before/after your match?
You can use something like: @"(\s|^)\*\w+\* (\s|$)", or
@"(\s|^)\*\w+\* (?=\s|$)" if you only want to allow spaces around your match,
but that won't find e.g. "*test*" within "'s either.

Niki

"Razzie" <ra****@quickne t.nl> wrote in message
news:eL******** *****@tk2msftng p13.phx.gbl...
Thanks Niki, that works! And looks better too :P

I'll have a look at Expresso too.

Thanks again!

"Niki Estner" <ni*********@cu be.net> wrote in message
news:eA******** ******@TK2MSFTN GP15.phx.gbl...
"Razzie" <ra****@quickne t.nl> wrote in
news:Os******** *****@TK2MSFTNG P11.phx.gbl...
Hey all,

Decided to give a shot at Regular expressions - need a bit of help :)

I can't seem to find the right regex for matching words like "*test*"
or *somevalue*" - in short, all words starting and ending with a *.

I tried things like string regex = @"(^*\|*^)") ;
but it still doesn't work completely (matches on "*test" too for
example). If anything could help me with this, that would be
appreciated.

You need to escape the asterix '*', because it's a regex metacharacter.
This pattern should work: @"\*\w+\*" (Read: Literally '*', followed by
at least one alphanumeric character, followed by another '*')
I've applied it to this post, and it only matched on the two samples
above

Also, does anyone have really GOOD regex tutorials? I read this one
http://www.regular-expressions.info/tutorial.html but that one is
(imho) still pretty hard to grasp.

Using Expresso (www.ultrapico.com) usually makes things a lot easier.
If you want a really good text on it, get J. Friedl's "Mastering Regular
Expressions".

Niki



Nov 16 '05 #5
Spaces are allowed, or linebreaks, for example when a *tag* is the only word
on the line. For example:

Customer *customer* has bought *product*.
*address*
This is a bogus line.

These should all match. However, it's not too important actually, since it
won't really happen that something is written like Hello*this*shou ld not
be*a*match*wher esoever.
So the solution you gave will work I guess, but I thought it was easy to NOT
have another char before or after a *, except spaces or linebreaks :)

"Niki Estner" <ni*********@cu be.net> wrote in message
news:ei******** ********@TK2MSF TNGP12.phx.gbl. ..
"Razzie" <ra****@quickne t.nl> wrote in
news:un******** ******@TK2MSFTN GP09.phx.gbl...
Hmm it's not 100% correct - it will match "*test*a" too (if there still
is a char after the last *.

Any ideas? (*^ doesn't seem to work either)


'^' will only match on the start of the string/line.
What kind of characters are allowed before/after your match?
You can use something like: @"(\s|^)\*\w+\* (\s|$)", or
@"(\s|^)\*\w+\* (?=\s|$)" if you only want to allow spaces around your
match, but that won't find e.g. "*test*" within "'s either.

Niki

"Razzie" <ra****@quickne t.nl> wrote in message
news:eL******** *****@tk2msftng p13.phx.gbl...
Thanks Niki, that works! And looks better too :P

I'll have a look at Expresso too.

Thanks again!

"Niki Estner" <ni*********@cu be.net> wrote in message
news:eA******** ******@TK2MSFTN GP15.phx.gbl...
"Razzie" <ra****@quickne t.nl> wrote in
news:Os******** *****@TK2MSFTNG P11.phx.gbl...
> Hey all,
>
> Decided to give a shot at Regular expressions - need a bit of help :)
>
> I can't seem to find the right regex for matching words like "*test*"
> or *somevalue*" - in short, all words starting and ending with a *.
>
> I tried things like string regex = @"(^*\|*^)") ;
> but it still doesn't work completely (matches on "*test" too for
> example). If anything could help me with this, that would be
> appreciated.

You need to escape the asterix '*', because it's a regex metacharacter.
This pattern should work: @"\*\w+\*" (Read: Literally '*', followed by
at least one alphanumeric character, followed by another '*')
I've applied it to this post, and it only matched on the two samples
above

> Also, does anyone have really GOOD regex tutorials? I read this one
> http://www.regular-expressions.info/tutorial.html but that one is
> (imho) still pretty hard to grasp.

Using Expresso (www.ultrapico.com) usually makes things a lot easier.
If you want a really good text on it, get J. Friedl's "Mastering
Regular Expressions".

Niki



Nov 16 '05 #6
"Razzie" <ra****@quickne t.nl> wrote in
news:uo******** ********@TK2MSF TNGP12.phx.gbl. ..
Spaces are allowed, or linebreaks, for example when a *tag* is the only
word on the line. For example:

Customer *customer* has bought *product*.
If this should match, punctuation-characters must be allowed too.
*address*
This is a bogus line.

These should all match. However, it's not too important actually, since it
won't really happen that something is written like Hello*this*shou ld not
be*a*match*wher esoever.
If you want to forbid exactly these, best would probably:
@"(?<!\w)\*\w+\ *(?!\w)"
(?<!\w) and (?!\w) tell the regex to match only if there is *no*
alphanumeric character before and after the string.
So the solution you gave will work I guess, but I thought it was easy to
NOT have another char before or after a *, except spaces or linebreaks :)
And dots, and commas, and hypens, probably also parens...

Usually, this is in fact easier: '\b' will match on word-boundary, so you
can use it for "match-whole-word" searches like @"\bSomeWord\b" .
Unfortunately ' *' isn't recognized as a word boundary, as neither ' ', nor
'*' are "word" (=alphanumeric) characters.

Niki

"Niki Estner" <ni*********@cu be.net> wrote in message
news:ei******** ********@TK2MSF TNGP12.phx.gbl. ..
"Razzie" <ra****@quickne t.nl> wrote in
news:un******** ******@TK2MSFTN GP09.phx.gbl...
Hmm it's not 100% correct - it will match "*test*a" too (if there still
is a char after the last *.

Any ideas? (*^ doesn't seem to work either)


'^' will only match on the start of the string/line.
What kind of characters are allowed before/after your match?
You can use something like: @"(\s|^)\*\w+\* (\s|$)", or
@"(\s|^)\*\w+\* (?=\s|$)" if you only want to allow spaces around your
match, but that won't find e.g. "*test*" within "'s either.

Niki

"Razzie" <ra****@quickne t.nl> wrote in message
news:eL******** *****@tk2msftng p13.phx.gbl...
Thanks Niki, that works! And looks better too :P

I'll have a look at Expresso too.

Thanks again!

"Niki Estner" <ni*********@cu be.net> wrote in message
news:eA******** ******@TK2MSFTN GP15.phx.gbl...
> "Razzie" <ra****@quickne t.nl> wrote in
> news:Os******** *****@TK2MSFTNG P11.phx.gbl...
>> Hey all,
>>
>> Decided to give a shot at Regular expressions - need a bit of help :)
>>
>> I can't seem to find the right regex for matching words like "*test*"
>> or *somevalue*" - in short, all words starting and ending with a *.
>>
>> I tried things like string regex = @"(^*\|*^)") ;
>> but it still doesn't work completely (matches on "*test" too for
>> example). If anything could help me with this, that would be
>> appreciated.
>
> You need to escape the asterix '*', because it's a regex
> metacharacter.
> This pattern should work: @"\*\w+\*" (Read: Literally '*', followed by
> at least one alphanumeric character, followed by another '*')
> I've applied it to this post, and it only matched on the two samples
> above
>
>> Also, does anyone have really GOOD regex tutorials? I read this one
>> http://www.regular-expressions.info/tutorial.html but that one is
>> (imho) still pretty hard to grasp.
>
> Using Expresso (www.ultrapico.com) usually makes things a lot easier.
> If you want a really good text on it, get J. Friedl's "Mastering
> Regular Expressions".
>
> Niki
>



Nov 16 '05 #7
I will try this as soon as I can :)

Thanks a lot!

"Niki Estner" <ni*********@cu be.net> wrote in message
news:eT******** ********@tk2msf tngp13.phx.gbl. ..
"Razzie" <ra****@quickne t.nl> wrote in
news:uo******** ********@TK2MSF TNGP12.phx.gbl. ..
Spaces are allowed, or linebreaks, for example when a *tag* is the only
word on the line. For example:

Customer *customer* has bought *product*.


If this should match, punctuation-characters must be allowed too.
*address*
This is a bogus line.

These should all match. However, it's not too important actually, since
it won't really happen that something is written like Hello*this*shou ld
not be*a*match*wher esoever.


If you want to forbid exactly these, best would probably:
@"(?<!\w)\*\w+\ *(?!\w)"
(?<!\w) and (?!\w) tell the regex to match only if there is *no*
alphanumeric character before and after the string.
So the solution you gave will work I guess, but I thought it was easy to
NOT have another char before or after a *, except spaces or linebreaks :)


And dots, and commas, and hypens, probably also parens...

Usually, this is in fact easier: '\b' will match on word-boundary, so you
can use it for "match-whole-word" searches like @"\bSomeWord\b" .
Unfortunately ' *' isn't recognized as a word boundary, as neither ' ',
nor '*' are "word" (=alphanumeric) characters.

Niki

"Niki Estner" <ni*********@cu be.net> wrote in message
news:ei******** ********@TK2MSF TNGP12.phx.gbl. ..
"Razzie" <ra****@quickne t.nl> wrote in
news:un******** ******@TK2MSFTN GP09.phx.gbl...
Hmm it's not 100% correct - it will match "*test*a" too (if there still
is a char after the last *.

Any ideas? (*^ doesn't seem to work either)

'^' will only match on the start of the string/line.
What kind of characters are allowed before/after your match?
You can use something like: @"(\s|^)\*\w+\* (\s|$)", or
@"(\s|^)\*\w+\* (?=\s|$)" if you only want to allow spaces around your
match, but that won't find e.g. "*test*" within "'s either.

Niki
"Razzie" <ra****@quickne t.nl> wrote in message
news:eL******** *****@tk2msftng p13.phx.gbl...
> Thanks Niki, that works! And looks better too :P
>
> I'll have a look at Expresso too.
>
> Thanks again!
>
>
>
> "Niki Estner" <ni*********@cu be.net> wrote in message
> news:eA******** ******@TK2MSFTN GP15.phx.gbl...
>> "Razzie" <ra****@quickne t.nl> wrote in
>> news:Os******** *****@TK2MSFTNG P11.phx.gbl...
>>> Hey all,
>>>
>>> Decided to give a shot at Regular expressions - need a bit of help
>>> :)
>>>
>>> I can't seem to find the right regex for matching words like
>>> "*test*" or *somevalue*" - in short, all words starting and ending
>>> with a *.
>>>
>>> I tried things like string regex = @"(^*\|*^)") ;
>>> but it still doesn't work completely (matches on "*test" too for
>>> example). If anything could help me with this, that would be
>>> appreciated.
>>
>> You need to escape the asterix '*', because it's a regex
>> metacharacter.
>> This pattern should work: @"\*\w+\*" (Read: Literally '*', followed
>> by at least one alphanumeric character, followed by another '*')
>> I've applied it to this post, and it only matched on the two samples
>> above
>>
>>> Also, does anyone have really GOOD regex tutorials? I read this one
>>> http://www.regular-expressions.info/tutorial.html but that one is
>>> (imho) still pretty hard to grasp.
>>
>> Using Expresso (www.ultrapico.com) usually makes things a lot easier.
>> If you want a really good text on it, get J. Friedl's "Mastering
>> Regular Expressions".
>>
>> Niki
>>
>
>



Nov 16 '05 #8

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

Similar topics

3
55390
by: Howard Dean | last post by:
I have a string with several carriage returns in it. For example: "This is my test string" I wish to convert it to "This is my test string" (remove all carriage returns. Can someone tell me how to do this using the string.Replace function? Thanks!
0
1579
by: Daniel Perron | last post by:
Hi, I was trying to use Galois.Net to specify code generators and I had to look for some text template tool to simplify the syntax. I was looking for something similar to the syntax of an Asp.Net page; for example, a template file could look like the following: <%@ Assembly Name="ReturnString.dll" %> <html> <title>Testing generator</title>
2
8108
by: live your lives | last post by:
i am trying to validate a simple username textbox using RegularExpressionValidator: TextBox tbUserName = new TextBox(); tbUserName.ID = "tbUserName"; string strPatternUserName = @"\W"; // i've tried using "\\W", "\w", "\\w","@\w", "/\\w", etc... // but it always prints my error msg UNLESS the textbox is set to "". // why does this not work?
18
3040
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How ??
0
1623
by: Tom Shelton | last post by:
This is a simple example of a numeric only text box. It allows only the entry of integer values. The advantage of this example is that it also handles user attempts to past non integer text data into the text box... Option Strict On Option Explicit On Imports System Imports System.Windows.Forms Imports System.ComponentModel
2
3842
by: Thomas T. Veldhouse | last post by:
Hello. I have been working with a client of mine on their seriazation code and we seem to have run into an issue when we migrated from .NET 1.1 to .NET 2.0. We have a framework of business objects that are serialized for persistant storage. The object graph DOES change as development continues, but it is required that we be able to deserialized existing classes and handle it properly (version tolerance). With version 1.1 of the .NET...
5
7191
by: FBergemann | last post by:
I use SunOS 5.8, gcc 3.3.2, boost 1.33.1. I have build the entire boost package and try to compile a simple example: #include <iostream> #include <string> #include <boost/regex.hpp // Boost.Regex lib using namespace std;
1
2151
by: Alan Patterson | last post by:
I have installed boost 1.33.1 and everything compiled and installed fine on Windows using Visual C++ 8.0. I wrote the following simple code to test the regex library and it doesn't seem to work. In the following "Matched" is not output to cout. Am I missing something or do I have a problem with my regex install? #include <string>
4
1503
by: bg_ie | last post by:
I have the following Regex - oldString = @"C:\ta"; Regex regexEndsInBackslash = new Regex(oldString + @"\\", RegexOptions.Singleline | RegexOptions.IgnoreCase); Any ideas why none of the following are true - if (regexEndsInBackslash.Match(@"C:\ta\").Success == true) ....
0
8997
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
8833
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9389
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
9335
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,...
0
9256
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8257
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6079
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
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3320
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

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.