Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression remove leading blank and dash character

Tags:

regex

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?"

like image 310
Ke. Avatar asked Jan 22 '23 10:01

Ke.


2 Answers

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.

like image 127
codaddict Avatar answered Mar 24 '23 20:03

codaddict


 $a=~s/- = - - //;
like image 40
muruga Avatar answered Mar 24 '23 21:03

muruga