Java: How to spit the line in between two pattern in Java - java

I am facing some problem with getting the text matching with the two patterns,Please help me out,Here is my question and my requirement :
Line : selenium.TypeKeys("name=email", "raj#ymail.com");
My requirement is to Get the name=email from the above line,
Thanks

This should give you the value between " and ,.
line.substring(line.indexOf("(\""), line.indexOf(","));

Related

Finding multiple groups in Java regex for simple option parser

I need to modify this regex to find multiple group matches:
(?:--)(?<key>[^\s=]+)(?:(?<assign> *[ =] *)(?! --)(?<value>"[^"]*"|\S+))?
In Java:
"(?:--)(?<key>[^\\s=]+)(?:(?<assign> *[ =] *)(?! --)(?<value>\"[^\"]*\"|\\S+))?"
This matches the following correctly:
--key=value
--key=--value
--key value
--flag
--key="--value"
--key "--value"
--key=value --foo=bar
--key=value --foo=bar --flag
But it fails if --flag comes before any other options:
--key=value --flag --foo=bar
I've been trying to modify the negative lookahead between the assign and value capture groups without success so far. The value captured for flag ends up being --foo=bar instead of null.
Any expert recommendations on how to solve this?
I managed to fix the regex. The website https://regexr.com/ was invaluable.
The fixed regex is:
(?<prefix>--)(?<key>[^\s=]+)(?:(?! --)(?<assign> *[ =] *)(?! --)(?<value>"[^"]*"|\S+))?
Here's the Java class and unit test:
https://gist.github.com/kirklund/845baf340a1999a57db9e59e6ba40ce0

Unknown inline modifier Java regex

i am parsing some text files in which the escape character is ? - There is also 4 escaped characters : + ' : ? . I think the clear escaping was working fine ( might be wrong tho ) until i added spring batch. Now when i try to launch the parsing of the file, this is failing and i don't get why.
Here is the error :
java.util.regex.PatternSyntaxException: Unknown inline modifier near index 6
(\?)(?|:|\+|')
And there is the code
private String regex = "(\\?)(?|:|\\+|')";
private String clearEscaping(String s){
return s.replaceAll(regex, "$2");
}
Can someone explain me why this is not working ? And how should i fix the issue ? Is there any more efficient way to do the escape clearing ?
Thanks to anubhava to point out that my regex was wrong in the 1st place & giving me the fix.
Thanks to Wiktor Stribiżew, for the complete solution & make it clear !
=> I changed my regex to "(\\\\?)([?:+'])"

Eclipse To replace two same line

By mistakenly i have replace couple of line in all the java files by using global replace (CTRL + H) function.
as currently text is as below :-
data.creationtime = DateUtils.convertDateTimeFromServer(data.creationtime);
data.creationtime = DateUtils.convertDateTimeFromServer(data.creationtime);
and i want to replace last line with correct word as below :-
data.creationtime = DateUtils.convertDateTimeFromServer(data.creationtime);
data.modificationtime = DateUtils.convertDateTimeFromServer(data.modificationtime);
i am not sure how to do it because i have two identical lines , can some one please guide me ?
i have followed this link but regex patterns is not working
SOLUTION
I have tried below pattern and it worked
For Match :-
(data.creationtime = DateUtils.convertDateTimeFromServer\(data.creationtime\);\s*?data.)([^ ]+?)( = DateUtils.convertDateTimeFromServer\(*?data.)([^ ]+?)(\);)
For Replace :- $1modificationtime$3modificationtime$5
Maybe not the most efficient way, but it should work.
Search-Pattern:
(data.creationtime = DateUtils.convertDateTimeFromServer\(data.creationtime\);\s*?data.)([^ ]+?)( = DateUtils.convertDateTimeFromServer\(data.creationtime\);)
Replacement-Pattern:
$1modificationtime$3
Demo:
https://www.myregextester.com/?r=da9d3e48

CSSSelector unable to locate element with name parameter

I have one page with 5 buttons and I need to find button with Name " Search "(with both leading and trailing whitespace)
When I try both these(printing name just for testing purpose) :
System.out.println(driver.findElements(By.cssSelector("a.gxi-button")).get(1).getText());
System.out.println(driver.findElement(By.cssSelector("a.gxi-button[name=' Search ']")).getText());
for first command its reutrning text " Search " but for second command its throwing exception " org.openqa.selenium.NoSuchElementException"
Can someone guide me what I am doing wrong here. Its seems that leading and trailing space is causing issue here. Can anyone guide how to deal with this?
Note: I have tried with both ' Search ' and 'Search' and in both case getting same exception
You just use *= to perform a partial matching.
System.out.println(driver.findElement(By.cssSelector("a.gxi-button[name*='Search']")).getText());
Or use XPath (if above still doesn't work)
driver.findElement(By.xpath(".//a[#class='gxi-button' and contains(#name, 'Search')]")).getText();
Or use XPath's normalize-space function
driver.findElement(By.xpath(".//a[#class='gxi-button' and normalize-space(#name)='Search']).getText();

convert at symbol ("#") to CharSequence

I am testing site with selenium and I need to send an e-mail to one of the fields. So far I am using this Java method:
String email = "test#example.com"
WebElement emailField = driver.findElement(By.id("mainForm:accountPanelTabId:1:accountEmails");
emailField.sendKeys(email);
But from (to me) uknown reason, this is sending exactly this value to the field:
testvexample.com
(so basically the "#" got replaced by "v")
Just out of curiosity: I am Czech and have Czech keyboard. One shortcut to write "#" symbol is rightAlt + v so I believe this can be connected...
So I am searching any "bulletproof" methot which always writes "#" symbol. Any help appreciated.
EDIT
the sendKeys is method of Selenium, and it simulates typing on keyboard. The javadoc is here: http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html#sendKeys%28java.lang.CharSequence...%29
The following should work: String email = "test\u0040example.com";
Apologies for misreading the question earlier.
I think you will have to call sendKeys using the proper values from the Keys enum to simulate the way you get your at-sign. Use Keys.ALT with your "v" in a chord:
sendKeys(Keys.chord(Keys.ALT, "v"));

Categories