Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern Matching (RegEx) 1

Status
Not open for further replies.

Extension

Programmer
Joined
Nov 3, 2004
Messages
311
Location
CA
Hi,

I need your Regex expertise. I'm trying to convert a string in the following format A1A1A1 to [/b]A1A 1A1[/b]. So I was looking for a simple Regex to grab the first three chracters as $1 and the last three as $2 and then change the format this way.

Any help would be really appreciated.

Thanks
 
Look at this
Code:
$line = "A1A1A1BBBCCC";
print "\$line=$line\n";

$line =~ s/(\S{3})/$1 /g;

print "\$line=$line\n";
 

Thanks mikrom. That's a quick and simple solution.

One more thing, the original format could be either A1A1A1 or A1A 1A1; so the RegEx is mainly to verify if it's in the right format. If not add a blank space after the three first characters.




 
Then correct it to
Code:
$line =~ s/(\S{3})\s*/$1 /g;

it formats e.g.
$line=A1A1A1BBBCCC
to
$line=A1A 1A1 BBB CCC

and the line
$line=A1A 1A1 BBB CCC
leaves it untouched. i.e.
$line=A1A 1A1 BBB CCC

however the line
$line=A1A 1A1 BBBCCC
will be corrected to
$line=A1A 1A1 BBB CCC
 
Thank you very much for your help Mikrom.
 
Status
Not open for further replies.

Similar threads

Replies
2
Views
347
  • Locked
  • Question Question
Replies
4
Views
468
  • Locked
  • Question Question
Replies
1
Views
307
  • Locked
  • Question Question
Replies
5
Views
450

Part and Inventory Search

Sponsor

Back
Top