I am trying to replace a character in a string with multiple occurences in Javascript.
String a1 = "There is a man over there";
when i use replace("e","x");
it will replace only the first occurrence of e.
So i am trying to use the g modifier like this replace(/e/g,"x");
But i am facing with this error Syntax error on tokens, Expression expected instead
I am not sure what i am doing wrong here.
replace(/e/g,"x") would be valid in JavaScript but not in Java. For Java just use the following:
String a1 = "There is a man over there";
String replaced = a1.replaceAll("e", "x"); // "Thxrx is a man ovxr thxrx"
The problem is you are mixing Java and Javascript which have absolutely nothing to do with each other.
Since you said you are trying in Javascript, do this:
var a1 = "There is a man over there"; // not String a1...
a1.replace(/e/g, 'x');
Related
I am working with some legacy code that has a static method call which we need to remove from our source tree.
The existing code is as follows:
Logger.getInstance(JdkUtil.forceInit(SomeBusiness.class));
What we need to end up with is:
Logger.getInstance(SomeBusiness.class);
I've spent all day today trying to figure out how to do that replacement. Since I have very little experience with regular expressions, I have only been able to come up with a pattern that matches the source string.
The pattern JdkUtil.forceInit([a-zA-Z_0-9]*.class) finds matches on the input string I am providing. I've tested this at https://www.freeformatter.com/java-regex-tester.html
So if anyone can post a Java solution to this, I would really appreciate it.
Below is some Groovy code that I have so far. What I am missing is to how correctly replacement explained above.
String source = 'Logger.getInstance(JdkUtil.forceInit(RtpRuleEngineCompiledImpl.class))'
String regexpPattern = 'JdkUtil.forceInit\\([a-zA-Z_0-9\\)]*.class\\)'
String replaced = source.replaceFirst(regexpPattern, 'hello')
println replaced
When I run the above code I get the following output:
Logger.getInstance(hello)
Obviously 'hello' is just for testing.
Thanks in advance to anyone who can give me some suggestions.
You'll likely want to do something such as:
class StackOverflow {
public static void main(String[] args) {
String source = "Logger.getInstance(JdkUtil.forceInit(RtpRuleEngineCompiledImpl.class))";
String regexpPattern = "JdkUtil.forceInit\\(([a-zA-Z_0-9]*.class)\\)";
String replaced = source.replaceFirst(regexpPattern, "$1");
System.out.println(replaced);
}
}
Result:
Logger.getInstance(RtpRuleEngineCompiledImpl.class)
The capture group ($1) replaces the entire string which was within the parentheses.
This is probably a pretty simple issue but since I'm working with 1.3 IDE I can't use the most common method to do this.
String at_cmd_response = atc.send("AT+CMGS=\"+35111111111\"\r");
I need to introduce a string called number which holds a number like "35191xxxxxxx" in at_cmd_response. To do so, I've seen the String.format method but I can't use it due to my IDE.
Is there another way to do this?
Thanks
Simple String concatenation (+) will work:
String at_cmd_response = atc.send("AT+CMGS=\""+number+"\"\r");
Its looks like you have a modem and want to send some commands... like send a SMS or make a phonecall or similar :-) ...
now to the question:
you need to concatenate the modem command with the parameter
in java those are strings and can be concatenated using the unary operator +
like:
"AT+CMGS=\"+yourPhoneNumber+"\"\r"
example:
String yourPhoneNumber = "+35111111111";
and now call the method
atc.send("AT+CMGS=\" + yourPhoneNumber + \"\r");
I use below code snippets for web element locators in Selenium.
Here variable is quoted with '" + text + "
String text="666";
String subject="Knowledge base '" + text + "' Approval Request";
Also for rest assured,
If I want to parameterize 3f1dd0320a0a0b99000a53f7604a2ef9 value of below URL.
https://pineapples.com/api/sn_sc/v1/fruit/items/3f1dd0320a0a0b99000a53f7604a2ef9/submit_producer
So I declared it in to a variable and using “+sys_ID+” I pass it.
String sys_ID = "3f1dd0320a0a0b99000a53f7604a2ef9";
RestAssured.baseURI = "https://pineapples.com";
RestAssured.basePath = "api/sn_sc/v1/fruits/items/"+sys_ID+"/submit_producer";
I have a list of strings: ab10sdj, ba1wqa, cd03asce, dfasc, etc. I'm looking to get the group of digits from the strings that start either with ab, or with ba.
So if a string starts with ab/ba, I need the group of digits right after them. If there a way for me to achieve this via a java matcher/regex?
Apologies for posting this as an answer but my current reputation level doesn't allow me 'comment' ... yet :)
As apposed to simply giving an answer to a relatively simple problem (without insulting your intelligence), I can help with diagnosing a solution. Try thinking the problem through in your mind, step by step. That is:
How do you test for the first 2 characters in a string?
If the test to point 1 passes (IE, they are ab or ba), how do you then process the rest of the string to test for 'digits' only?
How do you stop processing the 'digits' when you reach a non-digit?
Once you have your ab/ba prefix, only extracted the 'digits' immediately following the test condition, how will you handle the digits extracted?
Before considering placing the digits in a primitive type, you may wish to consider how many digits one may expect?
All the best with your code!
You can try regex like this :
public static void main(String[] args) {
// ab10sdj, ba1wqa, cd03asce, dfasc
String s1 = "ab10sdj";
String s2 = "ba1wqa";
String s3 = "cd03asce";
String s4 = "dfasc";
String pattern = "^(ab|ba)(\\d+).*";
System.out.println(s1.replaceAll(pattern, "$2")); // output 10
System.out.println(s2.replaceAll(pattern, "$2")); // output 1
System.out.println(s3.replaceAll(pattern, "$2")); // output cd03asce i.e, no change
System.out.println(s4.replaceAll(pattern, "$2")); // output dfasc i.e, no change
}
I am having regex expression problem. need helps from regex experts!
It's fairly simple but I can't get it to work.
I know if I want to check the starting of a text, I should use ^
and ending of the text, I should use $
I want to replace [quote] to <a>quote</a>.
This doesn't seems to work..
String test = "this is a [quote]"
test.replaceAll("^\\[", "<a>");
test.replaceAll("\\]$", "</a>");
I want the string to become "this is a <a>quote</a>"..
If you want to replace [ and ] with pair, you need to replace them in one time.
String test = "this [test] is a [quote]";
String result = test.replaceAll("\\[([^\\]]+)\\]", "<a>$1</a>");
^ implies that you are looking for something at the beginning of the string. However [ does not appear at the beginning of the string, so you will not have a match. Just do:
test.replaceAll("\\[", "<a>");
test.replaceAll("\\]", "</a>");
Also, you cannot modify a String in-place. you'll have to assign the output to something. You can do:
test = test.replaceAll("\\[", "<a>").replaceAll("\\]", "</a>");
That is if you still want to use the variable test.
I have the following REGEX that I'm serving up to java via an xml file.
[a-zA-Z -\(\) \-]+
This regex is used to validate server side and client side (via javascript) and works pretty well at allowing only alphabetic content and a few other characters...
My problem is that it will also allow zero lenth strings / empty through.
Does anyone have a simple and yet elegant solution to this?
I already tried...
[a-zA-Z -\(\) \-]{1,}+
but that didn;t seem to work.
Cheers!
UPDATE FOLLOWING INVESTIGATION
It appears the code I provided does in fact work...
String inputStr = " ";
String pattern = "[a-zA-Z -\\(\\) \\-]+";
boolean patternMatched = java.util.regex.Pattern.matches(pattern, inputStr);
if ( patternMatched ){
out.println("Pattern MATCHED");
}else{
out.println("NOT MATCHED");
}
After looking at this more closely I think the problem may well be within the logic of some of my java bean coding... It appears the regex is dropped out at the point where the string parse should take place, thereby allowing empty strings to be submitted... And also any other string... EEJIT that I am...
Cheers for the help in peer reviewing my initial stupid though....!
Have you tried this:
[a-zA-Z -\(\) \-]+