Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

RegEx Question  RSS feed

 
Venkat Sidh
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have following string called "ftrName" and here "v1.0" is variable how do I match this using regular expression.




I want to get output as "true" how do I get it. The highlighted (in bold) code doesn't seem to be working to match "v1.0". Any ideas? Thanks.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to get output as "true" how do I get it. The highlighted (in bold) code doesn't seem to be working to match "v1.0".


If you just want to match "v1.0", then just use "v1\\.0"... Otherwise, you actually need to know how the variable portion of the string will vary to create a regex for it.

Henry
 
Venkat Sidh
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry. Actually "v1.0" going to change dynamically. Actually I'm going to get array of file names with different version info. But I don't know what is the version, I know only first three strings, "appName", "siteName" and "featureName".

This version can be anything like v2.0, v3.0 or even version1,version2

so can we say [a-z|0-9|.] for this version part?

I'm sure that this version will have only alpha, numeric and "." (dot).



but its not working i'm not getting true.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the portion between the appname/sitename/ftrname and the ".jpg" could be anything... then use...



BTW, I recommend that you go back and reread regular expressions. It's much easier to use if you understand how regex works, than by trial and error.

Henry
 
Chris Rudd
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have a few problems here.

The first is that the string "a-z" does not match characters from a-z. In order to do that, it must be enclosed in brackets like [a-z].

Also, the dot "." must be escaped with a slash "\" - which in a Java String must also be escaped. Thus, to match a dot, you need "\\."

Finally, your group of optional terms will only match one character - you need a quantifier such as * (for zero or more matches) or a + (for one or more matches).

I'm no expert on regular expressions, but I always find this site helpful:
http://www.regular-expressions.info

As for the actual regex, you might want to try something like:



/edit
If you don't care what the version number format is, I would go with Henry's suggestion.

[ November 30, 2006: Message edited by: Chris Rudd ]
[ November 30, 2006: Message edited by: Chris Rudd ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!