With Regex Spliting the string into Alphanumeric and Numeric part


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting With Regex Spliting the string into Alphanumeric and Numeric part
# 1  
Old 06-30-2007
With Regex Spliting the string into Alphanumeric and Numeric part

Hi there

With shell script I'm trying to split the string into two parts. One is alphanumeric part, the other one is a numeric part.
Code:
dummy_postcode_1 = 'SL1' 
--> res_alpha = 'SL' and  res_numeric = '1' 
dummy_postcode_2 = 'S053' 
--> res_alpha = 'S' and  res_numeric = '053' 
dummy_postcode_3 = 'SO5R3' 
--> res_alpha = 'SO5R' and res_numeric = '3' 
dummy_postcode_4 = 'SO53R' 
--> res_alpha = 'SO53R' and res_numeric = ''
dummy_postcode_5 = '783652' 
--> res_alpha = '' and res_numeric = '783652' 

The logic of splitting these postcode strings is the rightmost alphanum character it seems and I could do that with the character checking but it seems to me this type of coding is a bit silly to do so.

Could you advise me how i can split them into two parts by using regex?

Thanks in advance,

Ozgur
# 2  
Old 06-30-2007
With ksh and bash (with extglog option enabled) you can do :

Code:
echo "SL1
S053
SO5R3
SO5R
783652" |
while read var
do
   res_alpha=${var%%*([0-9])}
   res_num=${var#$tes_alpha}
   echo "$var = '$res_alpha' + '$res_num'"
done

Output:
Code:
SL1 = 'SL' + '1'
S053 = 'S' + '053'
SO5R3 = 'SO5R' + '3'
SO5R = 'SO5R' + ''
783652 = '' + '783652'

With all shells you can also do the work with expr :
Code:
echo "SL1
S053
SO5R3
SO5R
783652" |
while read var
do
   res_alpha=`expr "$var" : '\(.*[^0-9]\+\)[0-9]*'`
   res_num=`expr "$var" : "${res_alpha}\(.*\)"`
   echo "$var = '$res_alpha' + '$res_num'"
done

Output:
Code:
SL1 = 'SL' + '1'
S053 = 'S' + '053'
SO5R3 = 'SO5R' + '3'
SO5R = 'SO5R' + ''
783652 = '' + '783652'


Last edited by aigles; 06-30-2007 at 11:06 AM.. Reason: add of expr solution
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep string with regex numeric characters

Hi all, I have the following entries in a file: Cause Indicators=80 90 Cause Indicators=80 90 Cause Indicators=82 90 Cause Indicators=82 90 Cause Indicators=82 90 The first 2 digits might change so I am after a sort of grep which could find any first 2 digits + the second 2,... (3 Replies)
Discussion started by: nms
3 Replies

2. Shell Programming and Scripting

Issue Spliting String in Python

I have a string like below Note: I have have a single to any number of comma "," seperated string assigned to jdbc_trgt variable. I need to split jdbc_trgt using comma(,) as the delimiter. I tried the below but it fails as i dont know how can i read each split string iterately. for... (4 Replies)
Discussion started by: mohtashims
4 Replies

3. Shell Programming and Scripting

Regex to exclude numeric

Dear All, My regex is like below. Its says all the number in coloum is include. 11666 11777 11888 ^(?\: (0|11)(666|777|888))\\d+$ How to exclude all the numeric that not mentioned in above regex. Regards, (3 Replies)
Discussion started by: tpx99
3 Replies

4. Shell Programming and Scripting

Generate Regex numeric range with specific sub-ranges

hi all, Say i have a range like 0 - 1000 and i need to split into diffrent files the lines which are within a specific fixed sub-range. I can achieve this manually but is not scalable if the range increase. E.g cat file1.txt Response time 2 ms Response time 15 ms Response time 101... (12 Replies)
Discussion started by: varu0612
12 Replies

5. Shell Programming and Scripting

parse a mixed alphanumeric string from within a string

Hi, I would like to be able to parse out a substring matching a basic pattern, which is a character followed by 3 or 4 digits (for example S1234 out of a larger string). The main string would just be a filename, like Thisis__the FileName_S1234_ToParse.txt. The filename isn't fixed, but the... (2 Replies)
Discussion started by: keaneMB
2 Replies

6. Shell Programming and Scripting

Spliting bash string into parts

Hello, let's say I have this string: string1="A\nB\nC D E\nFG\nH"; How can I split it so as to take every string separated with '\n' separately? For example, as for $string1, it would be split into string1_part1="A" string1_part2="B" string1_part3="C D E" string1_part4="FG"... (5 Replies)
Discussion started by: hakermania
5 Replies

7. Shell Programming and Scripting

using regex to get part of a string ?

Hi there, i wonder, is it possible to use regular expressions to partially select a string? I have a bunch of server names which look like this server1z-test server2z2 server45z-primary server13z3 I want to extract up to and including the 'z' in the server name, so for example ... (4 Replies)
Discussion started by: hcclnoodles
4 Replies

8. Shell Programming and Scripting

awk help with string spliting

Hello all, I am having a problem with awk's string split function. I have a string that has a number at the end, I am trying to remove the alpha portion of the string and just have the numeric part. Here is my code and the result: BEGIN { word = "$category121"; split(word, a, 121) print... (2 Replies)
Discussion started by: RobertSubnet
2 Replies

9. UNIX for Dummies Questions & Answers

AlphaNumeric String Operations

Hi :) I am writing a ksh I have a string of general format A12B3456CD78 the string is of variable length the string always ends with numbers (here it is 78.. it can be any number of digits may be 789 or just 7) before these ending numbers are alphabets (here it is CD can even be... (3 Replies)
Discussion started by: lakshmikanth
3 Replies

10. Shell Programming and Scripting

matching alphanumeric string

how to match an alphanumeric string like the following. i have to do like the following. if the input line is the data is {clock_91b} i have to replace that with the string was ("clock_91b") i tried like $line =~ s/the data is\s+\{(+)\}/the string was \(\"$1\"\)/ which... (4 Replies)
Discussion started by: sskb
4 Replies
Login or Register to Ask a Question