tags:

views:

428

answers:

6

Given a string like String a="- = - - What is your name?";

How to remove the leading equal, dash, space characters, to get the clean text,

"What is your name?"

A: 
 $a=~s/- = - - //;
muruga
A: 

If you want to remove the leading non-alphabets you can match:

^[^a-zA-Z]+

and replace it with '' (empty string).

Explanation:

  • first ^ - Anchor to match at the begining.
  • [] - char class
  • second ^ - negation in a char class
  • + - One or more of the previous match

So the regex matches one or more of any non-alphabets that are at the beginning of the string.

In your case case it will get rid of all the leading spaces, leading hyphens and leading equals sign. In short everything before the first alphabet.

codaddict
^\W+ generally does the same, maybe change the + into * (there might be nothing preceding)
NomeN
@NomenN: \W does not include digits, but [^a-zA-Z] can include digits. replacing + with * is not a good idea, say input is "Hi there" where I don't need to replace anything. But if I use * I'll still have a match (empty match) and I'll replace it with empty sting...resulting in no change. Using + will not match anything so nothing to replace...making it more efficient..preformance. wise.
codaddict
@codaddict I guess the digits part depends upon the requirements. Good explanation on the * vs +, I hadn't considered that.
NomeN
Thanks @codaddict, I don't want any digital. Your explanation is very clear.
Ke
+1  A: 

In Javascript you could do it like this

var a = "- = - - What is your name?";
a = a.replace(/^([-=\s]*)([a-zA-Z0-9])/gm,"$2");
Robusto
Why not just `a.replace(/^[-=\s]*/mg, "")`
KennyTM
My background thought was that he might not want this to work on blank lines, but your assumption set could also be right.
Robusto
+1  A: 

Java:

String replaced = a.replaceFirst("^[-= ]*", "");
Martin Wickman
I would change the + into *
NomeN
@NomeN: Why? If none of the offending characters are present--oh wait, I see @codaddict has already covered this.
Alan Moore
Got it ........
Martin Wickman
A: 

Assuming Java try this regex:

 /^\W*(.*)$/

retrieve your string from captured group 1!

\W* matches all preceding non-word characters
(.*)then matches all characters to the end beginning with the first word character

^,$ are the boundaries. you could even do without $ in this case.

Tip try the excellent Java regex tutorial for reference.

NomeN
Thanks @NomeN, the tutorial you recommend is excellent:)
Ke
A: 

In Python:

>>> "- = - - What is your name?".lstrip("-= ")
'What is your name?'

To remove any kind of whitespace, use .lstrip("-= \t\r\n").

Tim Pietzcker
Nope. `lstrip` takes a list of characters, not a regex.
Tim Pietzcker