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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex to remove charaters

Status
Not open for further replies.

uniopp

Technical User
Oct 7, 2001
152
JP
Hi,
I've been trying to find a regex that will remove all the character from my string after a specified point.
$string="some_thing";
I would like to remove the "_" and everything after it.
The string will be of varying length but the cut off point will always be the "_" character.
How can I do this?
In addition if the string contained more than one instance of the "_" character, how could I get it to cut off at the first one?
Thank you.
 
You could use a regex like this:
Code:
s/_.*$//;
[code]
but you might ilke to consider the substr and index solution:
[code]
substr($_,index($_,"_")) = "";
Personally I prefer the regex but I'm sure you'll find many here that prefer the substr.


Trojan.
 
Thank you.
I will use the regex at this stage to keep everything in my script the same (and it's easier for me to understand).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top