• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help needed in regex  RSS feed

 
Kannan dumeelKannan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

These are the values of my input strings

44 Indian abnk Colony Apt A ... B, Trichy TN

1 thillai nagar Ste 10,Apt 21 ... 30, Trichy TN

1 ... 99 Jthillai nagar, Trichy TN

I need to replace the range values in the above strings like A ... B and 21 ... 30 and 1 ... 99 with my new form values(in the below case 26)


I tried a regex ,givenstring.replaceAll("[0-9]+\\s...\\s[0-9]+", "26")); it worked for numbers ,given the alphabets also in the range ,it didn't work.Please help me with the solution
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kannan dumeelKannan wrote:Hi all,

These are the values of my input strings

44 Indian abnk Colony Apt A ... B, Trichy TN

1 thillai nagar Ste 10,Apt 21 ... 30, Trichy TN

1 ... 99 Jthillai nagar, Trichy TN

I need to replace the range values in the above strings like A ... B and 21 ... 30 and 1 ... 99 with my new form values(in the below case 26)


I tried a regex ,givenstring.replaceAll("[0-9]+\\s...\\s[0-9]+", "26")); it worked for numbers ,given the alphabets also in the range ,it didn't work.Please help me with the solution



Actually, for numbers, it didn't work either. It is just that you have a limited set of tests -- that you didn't have one that proved it would fail. Basically, the "." has special meaning in regex. It means any character, so your are matching "one or more digits, followed by a space, followed by any three characters, followed by a space, followed by one or more digits". Hence, this example will succeed...

1 thillai nagar Ste 10, Apt 21 and 30, Trichy TN

matching "21 and 30" as the range.

Henry

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!