matcher

How to appendReplacement on a Matcher group instead of the whole pattern?

ぐ巨炮叔叔 提交于 2019-12-02 19:14:06
I am using a while(matcher.find()) to loop through all of the matches of a Pattern. For each instance or match of that pattern it finds, I want to replace matcher.group(3) with some new text. This text will be different for each one so I am using matcher.appendReplacement() to rebuild the original string with the new changes as it goes through. However, appendReplacement() replaces the entire Pattern instead of just the group. How can I do this but only modify the third group of the match rather than the entire Pattern? Here is some example code: Pattern pattern = Pattern.compile("THE (REGEX)

Rspec view testing with capybara and rails3

烈酒焚心 提交于 2019-12-02 19:09:10
I really like the way RSpec is able to separate controller and view tests but have some problems with getting capybara matchers to work in a view test. What i basically try to achieve is sth like this: describe "some page" do it "should render with lots of stuff" do assign .. render rendered.should have_button ('Any button') #or any capybara matcher, really end end I've seen some posts on the net showing how to configure capybara and rails3 to work smoothly with cucumber or rspec controller tests, but this is not really what I want - that is, testing the views at the lowest level possible.

Regular expression - Greedy quantifier [duplicate]

陌路散爱 提交于 2019-12-02 17:41:55
问题 This question already has an answer here : SCJP6 regex issue (1 answer) Closed 4 years ago . I am really struggling with this question: import java.util.regex.*; class Regex2 { public static void main(String[] args) { Pattern p = Pattern.compile(args[0]); Matcher m = p.matcher(args[1]); boolean b = false; while(b = m.find()) { System.out.print(m.start() + m.group()); } } } When the above program is run with the following command: java Regex2 "\d*" ab34ef It outputs 01234456 . I don't really

How to show custom failure messages in ScalaTest?

家住魔仙堡 提交于 2019-12-02 17:22:15
Does anyone know how to show a custom failure message in ScalaTest? For example: NumberOfElements() should equal (5) Shows the following message when it fails: 10 did not equal 5 But i want more descriptive message like: NumberOfElements should be 5. You're the first to ask for such a feature. One way to achieve this is with withClue. Something like: withClue("NumberOfElements: ") { NumberOfElements() should be (5) } That should get you this error message: NumberOfElements: 10 was not equal to 5 If you want to control the message completely you can write a custom matcher. Or you could use an

How to replace a regexp group with a post proceed value?

荒凉一梦 提交于 2019-12-02 14:45:08
问题 I have this code to public static String ProcessTemplateInput(String input, int count) { Pattern pattern = Pattern.compile("\\{([^\\}]+)\\}"); Matcher matcher = pattern.matcher(input); while (matcher.find()) { String newelem=SelectRandomFromTemplate(matcher.group(1), count); } return input; } Input is: String s1 = "planets {Sun|Mercury|Venus|Earth|Mars|Jupiter|Saturn|Uranus|Neptune}{?|!|.} Is this ok? "; Output example: String s2="planets Sun, Mercury. Is this ok? "; I would like to replace

Regular expression - Greedy quantifier [duplicate]

余生颓废 提交于 2019-12-02 12:33:52
This question already has an answer here: SCJP6 regex issue 1 answer I am really struggling with this question: import java.util.regex.*; class Regex2 { public static void main(String[] args) { Pattern p = Pattern.compile(args[0]); Matcher m = p.matcher(args[1]); boolean b = false; while(b = m.find()) { System.out.print(m.start() + m.group()); } } } When the above program is run with the following command: java Regex2 "\d*" ab34ef It outputs 01234456 . I don't really understand this output. Consider the following indexes for each of the characters: a b 3 4 e f ^ ^ ^ ^ ^ ^ 0 1 2 3 4 5 Shouldn't

Android - Matcher.find() infinite

一世执手 提交于 2019-12-02 09:59:23
I have implemented AsyncTask where the regular expression provided by the user is being used for matching huge html-code data. However, because some of the regular expressions contain a lot of quantifiers/backtracking, Matcher.find() goes infinite. I have tried to use InterruptibleCharSequence provided here: How to terminate Matcher.find(), when its running too long? , but it seems that charAt is never get called, so never interrupted. My last guess is to create a new process just to run this regular matching procedure and then kill it when searching is cancelled. However, then I have a

Matcher not finding overlapping words?

你说的曾经没有我的故事 提交于 2019-12-02 09:17:44
I'm trying to take a string: String s = "This is a String!"; And return all 2-word pairs within that string. Namely: {"this is", "is a", "a String"} But right now, all I can get it to do is return: {"this is", "a String"} How can I define my while loop such that I can account for this lack of overlapping words? My code is as follows: (Really, I'd be happy with it just returning an int representing how many string subsets it found...) int count = 0; while(matcher.find()) { count += 1; } Thanks all. I like the two answers already posted, counting words and subtracting one, but if you just need a

How to replace a regexp group with a post proceed value?

谁说胖子不能爱 提交于 2019-12-02 07:30:39
I have this code to public static String ProcessTemplateInput(String input, int count) { Pattern pattern = Pattern.compile("\\{([^\\}]+)\\}"); Matcher matcher = pattern.matcher(input); while (matcher.find()) { String newelem=SelectRandomFromTemplate(matcher.group(1), count); } return input; } Input is: String s1 = "planets {Sun|Mercury|Venus|Earth|Mars|Jupiter|Saturn|Uranus|Neptune}{?|!|.} Is this ok? "; Output example: String s2="planets Sun, Mercury. Is this ok? "; I would like to replace the {} set of templates with the picked value returned by the method. How do I do that in Java1.5? Use

Matching decimals in strings using matcher()

旧巷老猫 提交于 2019-12-02 03:59:54
I have a question regarding the matcher. Currently I am trying to read a string and store all the digits into an array. My question is, how do you try to match both integers and decimals? I have an array of doubles called: double[] thisArray = new double[20]; Into this array, i am trying to store all the numbers I extract from the string. Matcher temp = Pattern.compile("(\d+)").matcher(x); That is my function for the matcher. But this only matches integers. I want to match both integers and decimals like (5.2). But how do I do this? I want to be able to enter in both integers and decimals into