473,654 Members | 3,097 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2501
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.tvy9w7siqnv 3q9@metalliumfr om 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.tvy9w7siqnv 3q9@metalliumfr om 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
1694
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 the word "train" but also "training"; this is giving me a lot of false positives. Can anyone assist? I tried a couple of things but nothing that works. Script is as follows: <?php
22
2855
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 in 1987) and I've tried to keep the spirit of C and make as few changes as possible. I was mostly driven by the bloat of C++ and, now, C99. I was also
3
9656
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... field1...field2...field3...
5
5869
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() = Microsoft.VisualBasic.Strings.Split(aLine, vbNewLine, -1, CompareMethod.Binary)
7
2253
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 getting the results I expect. Dim SA as string() Dim S as string S="FBE"
2
2187
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 = "field != value" result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
3
4861
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. Here is part of what I have. $myrow = mysql_fetch_assoc($rsindexpage); //function wordcount $wc = str_word_count($myrow); $half = ($wc/2);
5
2658
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 http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(vs.80).aspx but I want to get this list programmatically.
14
1718
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 illogically? Here's a test I just put together that splits on "&". The test strings are: "a&b" = (Correct!) I expect array length 2 and I get 2 "a&" = (Incorrect!) I expect array length 1 but I get 2 "a" = (Correct!) I expect array length 1 and...
0
8290
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
8815
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8707
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
8482
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
8593
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
7306
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...
1
6161
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2714
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.