473,383 Members | 1,984 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,383 software developers and data experts.

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 2288
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
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
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
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
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
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
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
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
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
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
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.