• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular Expression  RSS feed

 
Kendall White
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ive written the expression that matches my search criteria, but what i want to do after the expression evaluates to true is to return the matched value itself from the string... as far as i can find in the regex or string docs there isnt a method that will do that?

The problem is that ive had to quantify things in the expression like [.]+ [-]+ [0-9]+ with actuall amounts.. but you get the idea, so the matched criteria can be ambiguous.

I cant seem to find a tangible way to get the value out of the string when the value can change a fair bit, ive replaced and removed characters to try and get down to the criteria being the only thing left in the string but things like numbers that arent part of the criteria can often get prefixed or appended to it and it isnt working how I want it to...

Ideally what I want to do is just take the expression I wrote for the if statement, use the replaceAll method and add a ! to the expression as if it were logic... is there some way to make an entire expression negative? I tried for a while to nest with something like this [^[[0-9]+[-][0-9]{4,10}]] but the result just replaces the criteria itself... i dont think the ^ meta can be used on something other than directly on a character?

Is there some more advanced part of the pattern I can utilize to get exactly what I want or is there some way to turn an entire expression negative?

Kendall.

[ July 12, 2006: Message edited by: Kendall White ]
[ July 12, 2006: Message edited by: Kendall White ]
 
Henry Wong
author
Sheriff
Posts: 23291
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ive written the expression that matches my search criteria, but what i want to do after the expression evaluates to true is to return the matched value itself from the string... as far as i can find in the regex or string docs there isnt a method that will do that?


After you matched (assuming with find(), as matches() matches the whole string), you can get the matched string with the group() method. Furthermore, if you want a substring of that match, you can add "()" to your regex to create sub-groups that you can get the value of, also using the group() method.

Ideally what I want to do is just take the expression I wrote for the if statement, use the replaceAll method and add a ! to the expression as if it were logic... is there some way to make an entire expression negative? I tried for a while to nest with something like this [^[[0-9]+[-][0-9]{4,10}]] but the result just replaces the criteria itself... i dont think the ^ meta can be used on something other than directly on a character?


Not matching doesn't make sense, as what would the value be in the group if it didn't match? However, you can do this... search of a "" string which should match right at index 0 of the string -- then add a negative zero-length look-ahead for the component that you definitely want not present. This would cause the match to fail if it appears.

Henry
 
Kendall White
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had been using many groups within the patterns... but somehow I missed the bit about being able to retrieve them after the expressions has been used. Oh well at least I know now.

A few simple modifications and now its runing perfectly.

Thanks Henry.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!