-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
  • 89,455
  • 62
  • 89
  • 125
RIPAN
  • 3,326
  • 4
  • 17
  • 28

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
  • 18,127
  • 2
  • 19
  • 36
2

Try this one.If its working for you

^($|#|_)*

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

Prashant Deshmukh.....
  • 2,244
  • 1
  • 9
  • 11
1
s = s.replaceFirst("^[#_$]+", "");

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

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
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
  • 788
  • 8
  • 12