473,396 Members | 2,052 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,396 software developers and data experts.

Looking for tokens inside a string

Hello to all,
I'm trying to do a weird (?) thing with regexp and MySQL. For example,
i have a column with:

token1|token2|token3

(three tokens separated by pipes)
I'm trying to write an SQL query in order to look for the presence of
one of these tokens, that is "look for a string like 'tokenX' that is
preceded by the beginning of the line OR a pipe, AND that is followed
by the end of the line OR a pipe". I have tried this:

SELECT * FROM ... WHERE (... REGEXP "(^|\|)tokenX($|\|)");

but, of course, it doesn't work. So my question is: what's wrong ? My
regular expression, or my idea ?? There is a another way to obtain
what i want, that is to select a token within a string when tokens are
separated by anything but a comma ?

If anyone have an answer, =)

Serial
Jul 19 '05 #1
2 3493

Hi, Aurelien:

IMO, the answer depends on what rules you have governing the tokens.
For example, if you used literally the values "token1", "token2", and
"token3", there is no need to consider the pipes at all. Just use:

COLUMN LIKE '%token1%'

to look for token1 - it will not match any other tokens. Repeat for the
other tokens. If, however you are using tokens like "aaa" and "aaaaaa",
then you cannot use this method because "aaa" will also match inside of
"aaaaaa". In that case, you would have to do something like this to
find "aaa":

COLUMN LIKE 'aaa|%' or COLUMN LIKE '%|aaa' or COLUMN LIKE '%|aaa|%' OR
COLUMN = 'aaa"

I'd opt for the first method if possible, because as you can see it is
more efficient.

Hope that helps,
John
Aurelien Mazurie wrote:
Hello to all,
I'm trying to do a weird (?) thing with regexp and MySQL. For example,
i have a column with:

token1|token2|token3

(three tokens separated by pipes)
I'm trying to write an SQL query in order to look for the presence of
one of these tokens, that is "look for a string like 'tokenX' that is
preceded by the beginning of the line OR a pipe, AND that is followed
by the end of the line OR a pipe". I have tried this:

SELECT * FROM ... WHERE (... REGEXP "(^|\|)tokenX($|\|)");

but, of course, it doesn't work. So my question is: what's wrong ? My
regular expression, or my idea ?? There is a another way to obtain
what i want, that is to select a token within a string when tokens are
separated by anything but a comma ?

If anyone have an answer, =)

Serial

Jul 19 '05 #2

Hi, Aurelien:

IMO, the answer depends on what rules you have governing the tokens.
For example, if you used literally the values "token1", "token2", and
"token3", there is no need to consider the pipes at all. Just use:

COLUMN LIKE '%token1%'

to look for token1 - it will not match any other tokens. Repeat for the
other tokens. If, however you are using tokens like "aaa" and "aaaaaa",
then you cannot use this method because "aaa" will also match inside of
"aaaaaa". In that case, you would have to do something like this to
find "aaa":

COLUMN LIKE 'aaa|%' or COLUMN LIKE '%|aaa' or COLUMN LIKE '%|aaa|%' OR
COLUMN = 'aaa"

I'd opt for the first method if possible, because as you can see it is
more efficient.

Hope that helps,
John
Aurelien Mazurie wrote:
Hello to all,
I'm trying to do a weird (?) thing with regexp and MySQL. For example,
i have a column with:

token1|token2|token3

(three tokens separated by pipes)
I'm trying to write an SQL query in order to look for the presence of
one of these tokens, that is "look for a string like 'tokenX' that is
preceded by the beginning of the line OR a pipe, AND that is followed
by the end of the line OR a pipe". I have tried this:

SELECT * FROM ... WHERE (... REGEXP "(^|\|)tokenX($|\|)");

but, of course, it doesn't work. So my question is: what's wrong ? My
regular expression, or my idea ?? There is a another way to obtain
what i want, that is to select a token within a string when tokens are
separated by anything but a comma ?

If anyone have an answer, =)

Serial

Jul 19 '05 #3

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

Similar topics

10
by: Christopher Benson-Manica | last post by:
(if this is a FAQ, I apologize for not finding it) I have a C-style string that I'd like to cleanly separate into tokens (based on the '.' character) and then convert those tokens to unsigned...
6
by: Bill Cunningham | last post by:
Lexical generators such as Bison, Flex, Lex, &c produce C tokens for the parser or compiler. What do these C tokens look like? According to ANSI C, what does the standard have to say about C...
4
by: Jay Nesbitt | last post by:
Is there a quick way to extract tokens from a string of text? I know Java provides a StringTokenizer class to do this. Is there something similar in the dot net framework? Ex. string s = "some...
4
by: Jéjé | last post by:
Hi, I have a file which contain 1 pair of values by line like: Name1=Value1 = I nned to store these pair of values in a sortedlist. So the result expected for the 2 samples lines is: Key ...
22
by: Sven-Thorsten Fahrbach | last post by:
Hi Does anybody know of a library that offers a function to split pathnames. It should work somewhat like the following code snippet: ----------------- char *path =...
0
by: kamal9 | last post by:
#include <stdio.h> #include <string.h> void string2Lines(char line, char tokens){ char *token_ptr, token; char *i=";" " " "," "." "?" "!"; token_ptr = strtok(line, i ); while(token_ptr){...
5
by: gpaps87 | last post by:
hi, i wanted to know whether we can use strtok command to mark delimiters as tokens as well.In Java,we have a command: StringTokennizer(String str, String delimiters, boolean delimAsToken) ...
11
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
14
by: mdh | last post by:
Hi all, From p 125, gives rise to this issue for me. Is it true that a "token" in C ( philisophically ) is the least amount of digits/chars/underscores/*s ( and other non blank space that I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
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
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...
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.