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

How do I convert doashes to underscores?

290 100+
Hi,

I am trying to change dashes to underscores in text file.

The reason is that I am having problems processing some xml tags
so I want to change this <merchant-name> to <merchant_name>

To do this I used this regex:

Expand|Select|Wrap|Line Numbers
  1. $pattern = '#<(\w+)-(\w+)>#';
  2. $replacement = '<$1_$2>';
  3. $source = preg_replace( $pattern, $replacement, $source, -1 , $count); 
But for some reason, nothing is changed

Have I misunderstood something ?

The idea is that <anything-here> gets changed to <anything_here>

There is data between the tags that should not be touched, it is just the tag
names themselves that I want to change.

In the file, there about 2000 tags which need changing.

Can anyone see what I am doing wrong ?


Thanks.
Mar 19 '10 #1
2 1295
Atli
5,058 Expert 4TB
Hey.

A part from the tag name, are the tags empty? Are there any attributes?

You regular expression only matches a tag like:
- <xxx-yyy>

If it contains anything else, it will fail to match.
- <xxx-yyy >
- <xxx-yyy/>
- <xxx-yyy key="value">

To fix this, it needs to allow characters to trail the tag name
Expand|Select|Wrap|Line Numbers
  1. $pattern = '#<(\w+)-(\w+)([^>]*)>#';
  2. $replacement = '<$1_$2$3>';
Also note that if anything between the tag name and the closing > char contains a > char, this will cause a problem. (Like if the value of an attribute contains a > char.) - This is why it is recommended to use XML or HTML parsers to manipulate HTML rather than regular expressions. (HTML is not a "regular" markup.)
Mar 19 '10 #2
objx
1
Try this one. I tested it and it worked on my computer:


Expand|Select|Wrap|Line Numbers
  1. $source = preg_replace('#<([^>\-]+)-([^>]+)>#mU', "<$1_$2>", $source, -1 , $count);
Mar 20 '10 #3

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

Similar topics

6
by: Geoff Turner | last post by:
Using VB6, I set up a menu like &File ....&Open ....&Close etc. In design mode, the F is underscored as expected but O and C are not. In run mode no underscores are displayed when selecting...
31
by: Chris S. | last post by:
Is there a purpose for using trailing and leading double underscores for built-in method names? My impression was that underscores are supposed to imply some sort of pseudo-privatization, but would...
84
by: Andy Glew | last post by:
I am in search of any rigourous, scientific, academic or industrial studies comparing naming conventions in C++ or similar languages such as Ada: Specifically, are names formed with...
2
by: Kari Laitinen | last post by:
During the past 15 years I have been writing computer programs with so-called natural names, which means that the names (identifiers, symbols) in progarms are constructed of several natural words....
23
by: comp.lang.tcl | last post by:
I have a TCL proc that needs to convert what might be a list into a string to read consider this: ]; # OUTPUTS Hello World which is fine for PHP ]; # OUTPUT {{-Hello}} World, which PHP...
6
by: Kevin O'Brien | last post by:
Hey guys, I have a textbox where I want to find any spaces in the text and convert them to underscores _. Can someone please give some help with this one? Thank you, Kevin
2
by: nassim.bouayad.agha | last post by:
Hello, here is a code snippet showning my problem : template<typename _K> class TClass1 { public: void Process(const _K& arg) const {
20
by: benhoyt | last post by:
Hi guys, I've been using Python for some time now, and am very impressed with its lack of red tape and its clean syntax -- both probably due to the BDFL's ability to know when to say "no". ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
0
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...

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.