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

split keywords into array

Does anyone know what regular expression I would use to split text into
an array, assuming the text can be separated by any non alphanumeric
character?

e.g

$string ="cat,dog fish, mouse -elephant/tiger";

would give

Array
(
[0] =cat
[1] =dog
[2] =fish
[3] =mouse
[4] =elephant
[5] =tiger
)
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 24 '07 #1
3 2488
Rik
On Tue, 24 Jul 2007 18:11:53 +0200, Geoff Berrow <bl******@ckdog.co.uk
wrote:
Does anyone know what regular expression I would use to split text into
an array, assuming the text can be separated by any non alphanumeric
character?
$array = preg_split('/\W+/',$string,-1,PREG_SPLIT_NO_EMPTY);

\W = non-word character

"A "word" character is any letter or digit or the underscore character,
that is, any character which can be part of a Perl "word". The definition
of letters and digits is controlled by PCRE's character tables, and may
vary if locale-specific matching is taking place. For example, in the "fr"
(French) locale, some character codes greater than 128 are used for
accented letters, and these are matched by \w."

--
Rik Wasmus
Jul 24 '07 #2
Message-ID: <op.tvy9w7siqnv3q9@metalliumfrom Rik contained the
following:
>
>Does anyone know what regular expression I would use to split text into
an array, assuming the text can be separated by any non alphanumeric
character?

$array = preg_split('/\W+/',$string,-1,PREG_SPLIT_NO_EMPTY);
Thanks Rik. :-)
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 24 '07 #3
Rik
On Tue, 24 Jul 2007 19:12:53 +0200, Geoff Berrow <bl******@ckdog.co.uk
wrote:
Message-ID: <op.tvy9w7siqnv3q9@metalliumfrom Rik contained the
following:
>>
>>Does anyone know what regular expression I would use to split text into
an array, assuming the text can be separated by any non alphanumeric
character?

$array = preg_split('/\W+/',$string,-1,PREG_SPLIT_NO_EMPTY);

Thanks Rik. :-)

No problem.
BTW, if you also want to split on the underscore:
$array = preg_split('/(\W|_)+/',$string,-1,PREG_SPLIT_NO_EMPTY);
--
Rik Wasmus
Jul 24 '07 #4

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

Similar topics

2
by: google | last post by:
I have a script which parses the BBC RSS feed once an hour, and drops any stories with certain keywords into a database. I would like to ban certain strings; at the moment the script will pick up...
22
by: Michael Nahas | last post by:
Antti & all interested, The draft description of my language to replace C is available at: http://nahas.is-a-geek.com/~mike/MyC.pdf I am a long time C programmer (I read the old testament...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
5
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf) Dim csvColumns2 As String() =...
7
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not...
2
by: Digital Fart | last post by:
following code would split a string "a != b" into 2 strings "a" and "b". but is there a way to know what seperator was used? string charSeparators = { "=", ">=", "<=" , "!=" }; string s1 =...
3
by: ashok | last post by:
Hi, I need a function that will divide text from mysql in 2 parts, so that I can display first half in one column and second half in second column. I can't find what function will do this job....
5
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
How can I get the list of connection string's keywords available in sqlclient programmatically? I have found the list in here...
14
by: Stevo | last post by:
If you split a string into an array using the split method, it's not working the way I'd expect it to. That doesn't mean it's wrong of course, but would anyone else agree it's working somewhat...
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: 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
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
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...
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
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...
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.