473,498 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RegEx - Translate

Hi,

I want to categorize using one Regular Expression:

input: "Earth"
output: "Planet"

input: "Moon"
output: "Planet"

input: "Cat"
output: "Animal"

input: "Dog"
output: "Animal"

input: "Tomato"
output: "Vegetable"

input: "Carrot"
output: "Vegetable"

And if no match is found an empty string
input: "^%&%#"
output: ""

Something like this:
"if (Earth|Moon) then Planet ElseIf(Cat|Dog) then Animal... Else "" "

Any ideas?

- Thomas
Jul 21 '05 #1
7 2290
Thomas <th****@corpuslogic.com> wrote:
I want to categorize using one Regular Expression:


Is there any reason you particularly want to use a regular expression
here rather than something else which is more suitable, like a
Hashtable?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Thomas <th****@corpuslogic.com> wrote:
I want to categorize using one Regular Expression:


Is there any reason you particularly want to use a regular expression
here rather than something else which is more suitable, like a
Hashtable?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
Yes - I want the espression to be stored as a dynamic setting for the
application.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Thomas <th****@corpuslogic.com> wrote:
I want to categorize using one Regular Expression:


Is there any reason you particularly want to use a regular expression
here rather than something else which is more suitable, like a
Hashtable?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #4
Thomas <th****@corpuslogic.com> wrote:
Yes - I want the espression to be stored as a dynamic setting for the
application.


Why not store the mapping as a dynamic setting instead, and load that
in at runtime? Certainly if you're using XML for your configuration it
should be very easy to work out a mapping schema - and most other
configuration techniques make it quite easy too.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5
> Yes - I want the espression to be stored as a dynamic setting for the
application.

Hi,

I am curious what that has to do with a regular expression, can you tell us
how you have planned to do this?

Cor
Jul 21 '05 #6
Please illustrate with an example - I'm very interested!

The setting must be stored as a single string - but can be structured - eg
with XML.

This is the current setting I have come up with:

This will categorize company name endings into LLC, BUSINES_CORP etc. I had
to escape the >< to store in an attribute.

<add key="Designations.Match"
value="(?&lt;LLC&gt;\bLimited\s+Company\b\W*$)|(?& lt;LLC&gt;\bLimited\s+Liab
ility\s+Company\b\W*$)|(?&lt;LLC&gt;\bLC\b\W*$)|(? &lt;LLC&gt;\bLLC\b\W*$)|(?
&lt;BUSINESS_CORP&gt;\bCorporation\b\W*$)|(?&lt;BU SINESS_CORP&gt;\bIncorpora
ted\b\W*$)|(?&lt;BUSINESS_CORP&gt;\bLimited\b\W*$) |(?&lt;BUSINESS_CORP&gt;\b
Corp\b\W*$)|(?&lt;BUSINESS_CORP&gt;\bInc\b\W*$)|(? &lt;BUSINESS_CORP&gt;\bLtd
\b\W*$)|(?&lt;BUSINESS_CORP&gt;\bCorporation\b\W*$ )|(?&lt;PROFESSIONAL_CORP&
gt;\bProfessional\s+Corporation\b\W*$)|(?&lt;PROFE SSIONAL_CORP&gt;\bProfessi
onal\s+Corporation\b\W*$)|(?&lt;PROFESSIONAL_CORP& gt;\bProfessional\s+Corp\b
\W*$)|(?&lt;PROFESSIONAL_CORP&gt;\bProf\s+Corp\b\W *$)|(?&lt;PROFESSIONAL_COR
P&gt;\bPC\b\W*$)|(?&lt;PROFESSIONAL_CORP&gt;\bPC\b \W*$)|(?&lt;LLP&gt;\bRegis
tered\s+Limited\s+Liability\s+Partnership\b\W*$)|( ?&lt;LLP&gt;\bLLP\b\W*$)|(
?&lt;LLP&gt;\bLimited\s+Liability\s+Partnership\b\ W*$)|(?&lt;LLP&gt;\bLimite
d\s+LP\b\W*$)|(?&lt;LLP&gt;\bLimited\s+LP\b\W*$)|( ?&lt;LLLP&gt;\bRegistered\
s+Limited\s+Partnership\b\W*$)|(?&lt;LLLP&gt;\bLim ited\s+Partnership\b\W*$)|
(?&lt;LLLP&gt;\bLP\b\W*$)|(?&lt;LLLP&gt;\bRegister ed\s+Limited\s+Liability\s
+Limited\s+Partnership\b\W*$)|(?&lt;LLLP&gt;\bLimi ted\s+Liability\s+Limited\
s+Partnership\b\W*$)|(?&lt;LLLP&gt;\bLimited\s+LLP \b\W*$)|(?&lt;LLLP&gt;\bLL
LP\b\W*$)" />
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Thomas <th****@corpuslogic.com> wrote:
Yes - I want the espression to be stored as a dynamic setting for the
application.


Why not store the mapping as a dynamic setting instead, and load that
in at runtime? Certainly if you're using XML for your configuration it
should be very easy to work out a mapping schema - and most other
configuration techniques make it quite easy too.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #7
Thomas <th****@corpuslogic.com> wrote:
Please illustrate with an example - I'm very interested!

The setting must be stored as a single string - but can be structured - eg
with XML.


Okay, here's an example XML format you could use:

<map>
<mapping from="Something" to="Another" />
<mapping from="Earth" to="Planet" />
<mapping from="Moon" to="Planet" />
</map>

You'd then convert the XML into a hashtable in any of various ways - eg
loading it into an XmlDocument and using XPath to find all the mapping
nodes.

Your conversion would then just be:

public string Convert (string input)
{
if (map.ContainsKey(input))
{
return (string)map[input];
}
// This would depend on exactly what you wanted to do with
// non-matching input
return input;
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #8

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

Similar topics

3
412
by: Felix Schwarz | last post by:
Hi all, I'm experiencing problems with a regular expression and I can't figure out which words I use when googling. I read the python documentation for the re module multiple times now but still...
4
18577
by: Michael Vilain | last post by:
Originally, I was using $value =~ s/<.*>//g; to strip HTML tags from a variable. It actually stripped everything from the first "<" to the last ">" after the ending tag. I found this regex...
4
4521
by: Brian Henry | last post by:
I have phone numbers like this in a data table 123-435-1234 1231231234 432.234.2321 they all have different formatting, what I want to do is get them all formatted like this (123) 123-1234
13
2341
by: Chris Lieb | last post by:
I am trying to write a regex that will parse BBcode into HTML using JavaScript. Everything was going smoothly using the string class replace() operator with regex's until I got to the list tag....
5
1441
by: Peter | last post by:
I'm wondering if someone can tell me whether the following set of regex substitutions is possible. I want to convert parallel legal citations into single citations. What I mean is, I want to...
5
1700
by: skavan | last post by:
Hi, I'm just wrapping my head around regex and am pretty sure it can do the task at hand - but it's too complex for my brain to process -- so am throwing it out there for you experts to comment...
7
3270
by: AMP | last post by:
Hello, I have a long string that I need to take a section out of and use. The section I need always starts with "XX XX XX XX XX" and always ends with "YY YY YY YY YY" I want to throw away the...
5
7168
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 //...
10
3823
by: igor.kulkin | last post by:
I have a small utility program written in Python which works pretty slow so I've decided to implement it in C. I did some benchmarking of Python's code performance. One of the parts of the program...
4
2423
by: sherifffruitfly | last post by:
Hi all, I can't see what's wrong with this regex pattern: private int ParsePageViews(string str) { int ret = 0; string pattern = @"Visits.*\n\s*Total\s\.*\s(? <visits>(\d{3})|(\d,\d{3}))";
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7203
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...
0
7379
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...
1
4908
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...
0
3093
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...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
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 ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
290
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...

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.