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

extract numbers and text from a long string

I have a long string from which I would like to separate words and the next numbers and store them in arrays if possible

String = aa12ab123ac34ad23ae321ca21cb234za123zb12zd12

text usually is 2 characters long but the digits vary in length 2-4 digits
I would like to store them as
alpha[1]=aa
number[1]=12
alpha[2]=ab
number[2]=123
.
.
.
alpha[n]=zd
number[n]=12
Oct 24 '13 #1
6 1896
RonB
589 Expert Mod 512MB
We would like to help you troubleshoot your code, but you first need to show us that code and what it outputs and how that output differs from what you expected.
Oct 25 '13 #2
Frinavale
9,735 Expert Mod 8TB
Consider using regular expressions to retrieve letter and number groupings.

-Frinny
Oct 25 '13 #3
RonB
589 Expert Mod 512MB
And consider using the push function to add the groups to the arrays.
Oct 25 '13 #4
Had fun Figuring it out myself. I just changed the logic a bit to use 2 arrays but skipping the i values in the for loop.

Expand|Select|Wrap|Line Numbers
  1.  my $data = 'aa12ab123ac34ad23ae321ca21cb234za123zb12zd12'
  2.   my @values1 = split(/(\d+)/, $data);
  3.   my @values2 = split(/(\d+)/, $data);
  4.  
  5. for (my $i=0; $i < @values1; $i++) {
  6.   if ($values1[$i] eq "aa"){
  7.    if ($values2[$i+1] == 0){
  8.       print "Yay\n";
  9.        }
  10.     }
  11.  }
Not the best way i know, but it does the job. I wanted separate arrays for string and number earlier.

I might ask questions on tk or tkx as I intend to display this parser's result on gui. Thanks.

If you guys can think of cleaner/simpler method, please let me know. Kindly try to give a code snippet, if possible. Else point to a topic :)
Oct 25 '13 #5
Frinavale
9,735 Expert Mod 8TB
It's not quite what we recommended but it solves the problem. There's always more than one solution to the problem :)

-Frinny
Oct 28 '13 #6
use the following code
Expand|Select|Wrap|Line Numbers
  1. my $data = 'aa12ab123ac34ad23ae321ca21cb234za123zb12zd12';
  2. while($data =~ m/([a-z]+)(\d+)/igs)
  3. {
  4.     print "alpha  :: $1\n";
  5.     print "Number :: $2\n";
  6. }
Jul 23 '14 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Thomas Matthews | last post by:
Hi, My goal is to extract a text field into a string type. The text field is from a buffer (array) of characters and delimited by white space or double quotes. *** I'm looking to perform this...
44
by: RB | last post by:
How to extract bytes from long, starting from the last byte? For example, I have a long number: 0x12345678 I need to represent it as the following bytes list: 0x78, 0x56, 0x34, 0x12 Thanks in...
4
by: Guogang | last post by:
Hi, I need to extract plain text from HTML page (i.e. do not show images, html formatting, ...) Is there some C# class/function that can help me on this? Thanks, Guogang
4
by: Ryan | last post by:
I am trying to write a c# function to extract all numbers out of a string. What is the easiest way to do this, regular expressions? I must account for numbers formatted with or without commas...
3
by: Jay Douglas | last post by:
Hello all. I'm having a hard time coming up with a regular expression that can extract just the numbers out of string into a GroupCollection. Any help is appreciated.
6
by: Dave | last post by:
Hope someone can help! I have a memo fiels in which there are a few numbers including dates but what I want to do is extract a number which is 6 figures long. Can anyone help me? Thanks Dave
7
by: Malcolm | last post by:
This is a program to convert a text file to a C string. It is offered as a service to the comp.lang.c community. Originally I thought it would be a five minute job to program. In fact there are...
5
by: deko | last post by:
If I have random and unpredictable user agent strings containing URLs, what is the best way to extract the URL? For example, let's say the string looks like this: registered NYSE 943 <a...
1
by: GS | last post by:
I need to extract sections out of a long string of about 5 to 10 KB, change any date format of dd Mmm yyyy to yyyy-mm-dd, then further from each section extract columns of tables. what is the...
2
by: jmartmem | last post by:
Is there a function, or a combination of functions, in MS Access 2007 that will allow me to extract part of a string? I have a database text field "Email" that contains a person's name and email...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.