-4

I am running through a scenario where i have to create table through jdbc statement and i am receiving the table name as method argument. Oracle database is used and Oracle has naming rule like table name should not start with any of these following character $#_. I want to replace all those special character only from the beginning but not from in_between or at the end. For example:

#_PHYSICIANSPHYSICIANS

##_#_PHYSICIANSPHYSICIANS

##_#_PHYSICIANS_#_NAMEPHYSICIANS_#_NAME

What Regular expression shall I use to replace these characters from the beginning?

Toto
  • 86,179
  • 61
  • 85
  • 118
RIPAN
  • 2,656
  • 4
  • 16
  • 26
  • 2
    What have you tried so far ? Where are you facing an issue ? We're not here to do your job, There are a lot of site where you can test and design your regex. Comeback and Ask when you have a precise problem, or something you don't understand. not when you want someoine else to do your job – vincrichaud Dec 19 '18 at 13:05
  • @vincrichaud sadly these posts will never stop while people are still doing there job for them anyway. I wish people would not answer these types of questions. – Martyn Dec 19 '18 at 14:05

4 Answers4

4

Whatever characters you want to strip from the beginning, you can put them in the character set and use this regex,

^[#_]+

and replace it with empty string.

Here ^ marks the start of string and [#_]+ means one or more characters from character set.

Java code,

List<String> list = Arrays.asList("#_PHYSICIANS","##_#_PHYSICIANS","##_#PHYSICIANS_#_NAME");
list.forEach(x -> System.out.println(x + " --> " + x.replaceAll("^[#_]+", "")));

Prints,

#_PHYSICIANS --> PHYSICIANS
##_#_PHYSICIANS --> PHYSICIANS
##_#PHYSICIANS_#_NAME --> PHYSICIANS_#_NAME
Pushpesh Kumar Rajwanshi
  • 17,308
  • 2
  • 17
  • 34
2

Try this one.If its working for you

^($|#|_)*

https://regex101.com/r/wg9TEe/1

Prashant Deshmukh.....
  • 2,184
  • 1
  • 8
  • 10
1
s = s.replaceFirst("^[#_$]+", "");

^ means from the beginning, [...] lists the chars, ...+ one or more.

Joop Eggen
  • 102,262
  • 7
  • 78
  • 129
1

This removes the part before any character.

 String text = "#_PHYSICIANS_#";
 Pattern p = Pattern.compile("\\p{L}");
 Matcher m = p.matcher(text);
 if (m.find()) {
   System.out.println(m.start());
   String result = text.substring(m.start(), text.length());
   System.out.println(result);
 }
Serhat Oz
  • 758
  • 7
  • 12