I am getting stuck in this situation.
public void findListOfPattern(){
String text = "abce1213abcd231asdf";
String find = "1213|231|1232";
Pattern part = Pattern.compile(find);
Matcher mat = part.matcher(text);
System.out.println(mat.find()); //True
}
Able to get true result if any of string in find get match.
I want list of matcher from text.
There text can large with more find string and also find string can more.
In find : 1213,231,1232 are separates.
Result should be like :- 1213,231
You need to invoke mat.group() to return the desired match.
Typically you'd loop until mat.find() returns true and print all matches successively by invoking mat.group().
You can then build your expected result String by concatenating the outcome of mat.group() as you wish, e.g. with a StringBuilder.
Notes
API here.
You need to invoke Matcher#find in order for Matcher#group to yield any result and not throw IllegalStateException
Your Pattern only has the default group. If you'd used parenthesis or named groups (from Java 7), you could also invoke overloads Matcher#group(int group) or Matcher#group(String name).
Related
I want to get one particular word using regex in java. thanks
in the below paragraph, I need to find the network interface name
resource "azurerm_network_interface" "nic_LinuxVMCent-nhi" {
name = "nic_LinuxVMCent-nhi"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
ip_configuration {
name = "pubIP_LinuxVMCent-nhi"
subnet_id = azurerm_subnet.sub_wind12VM-PtN.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.pubIP_LinuxVMCent-nhi.id
}
}
data "azurerm_snapshot" "snapLinuxVMCent-nhi" {
name = "CentOS76New-0"
resource_group_name = "SaaSworkloadsnaps"
}
Expected Result Ex:
nic_LinuxVMCent-nhi
This is a multi-line bit of text. However, there appears to be a line which you could recognise with a regex:
resource "azurerm_network_interface" "nic_LinuxVMCent-nhi" {
So the regex for that would be ^resource "azurerm_network_interface" "([^"]+)" {$ - see https://regexr.com/67ldb
You can use Matcher.match to see if the any line matches this expression and if it does then matcher.group(1) will be the value you're looking for.
you can use this regex to find the network interface name:
(?<=resource \"azurerm_network_interface\" \").+(?=\" {)
I have used lookahead to find the name.
Also, here's a link to regex101:
Link
I don't know network interfaces so,
This regex solution is specific to "azurerm_network_interface."
If you need any additional help, please comment down below.
Cheers :)
I am implementing as automation script and following BDD frame work with selenium webdriver.
Acceptance Criteria:
Scenario: Members name
Given that the web page is displayed
When the user clicks anywhere on the member row
Then member First Name, Middle Initial, Last Name will be displayed
And Member First Name, Middle Initial, Last name display in Camel case
Would you pleas let me know how to validate the Camel case for displayed information in web page? Like "Jhon D Hamton".
By using WordUtils in the Apache Commons lang library:
Specifically, the capitalizeFully(String str, char[] delimiters) method should do the job:
String blah = "LORD_OF_THE_RINGS";
assertEquals("LordOfTheRings", WordUtils.capitalizeFully(blah, new char[]{'_'}).replaceAll("_", ""));
Resource Link:
What is the simplest way to convert a Java string from all caps (words separated by underscores) to CamelCase (no word separators)?
Well, assuming you can grab your string, I'd split it then check that the first letter is uppercase:
boolean isCamelCase(String s) {
String[] split_string = s.split(" ");
for(int i=0;i<split_string.length;i++) {
if(!Character.isUpperCase(split_string[i].charAt(0))){
return false;
}
}
return true;
}
If you know how, or are able to get the entire name as a string, you can use the following Regex to assert that it is camel case:
[A-Z]([A-Z0-9]*[a-z][a-z0-9]*[A-Z]|[a-z0-9]*[A-Z][A-Z0-9]*[a-z])[A-Za-z0-9]*
This matches strings that start with an uppercase letter, contain only letters and numbers, and contain at least one lowercase letter and at least one other uppercase letter.
For Java, you want to call the String class's matches() method, which returns true or false:
boolean doesItMatch = yourString.matches('theRegexAbove');
I wanna detect exact domain url in string and then change that with another string and finally make it clickable in TextView.
What I want:
this is sample text with one type of url mydomain.com/pin/123456. another type of url is mydomain.com/username.
Wel, I wrote this regex:
([Hh][tT][tT][pP][sS]?://)?(?:www\\.)?example\\.com/?.*
([Hh][tT][tT][pP][sS]?://)?(?:www\\.)?example\\.com/pin/?.*
this regex can detect:
http://www.example.com
https://www.example.com
www.example.com
example.com
Hhtp://www.example.com // and all other wrong type in http
with anything after .com
Issues:
1. How detect end of domain ( with space or dot)
2. How detect two type of domain, one with /pin/ and another without?
3. How to replace detected domain like mydomain.com/pin/123 with PostLink and mydomain.com/username with ProfileLink
4. I know how to make them clickable with Linkify but if it possible show me best way to provide content provider for links to open each link with proper activity
You could try:
([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,#?^=%&:/~+#-]*[\w#?^=%&/~+#-])?
which is a regex I found after a quick search here on stackoverflow:
Regular expression to find URLs within a string
I just removed the http:// part of that regex to fit your needs.
Be aware though that because of that it now tracks everything that is connected with a dot and no whitespace. For example: a.a would also be found
With special thanks of Gildraths
Answer to question 1
String urlRegex = "(https?://)?(?:www\\.)?exampl.com+([\\w.,#?^=%&:/~+#-]*[\\w#?^=%&/~+#-])?";
Pattern pattern = Pattern.compile(urlRegex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(textString);
Answer to question 2, 3
while(matcher.find()){
// Answer to question 2 - If was true, url contain "/pin"
boolean contain = matcher.group().indexOf("/pin/") >= 0;
if(contain){
String profileId = matcher.group().substring(matcher.group().indexOf("/pin/") + 5, matcher.group().length());
}
// Answer to question 3 - replace match group with custom text
textString = textString.replace(matcher.group(), "#" + profileId);
}
Answer to question 4
// Pattern to detect replaced custom text
Pattern profileLink = Pattern.compile("[#]+[A-Za-z0-9-_]+\\b");
// Schema
String Link = "content://"+Context.getString(R.string.profile_authority)+"/";
// Make it linkify ;)
Linkify.addLinks(textView, profileLink, Link);
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 write a simple Rss Feed reader now i have this problem, in the item description i have the text but this text have this caracters <br/>
for example
"my dog <br/> is black and he <br/> stay on table "
now i wont to clear the string from this caracters, i wirte this metod
private static String IsMatch(String s, String pattern) {
try {
Pattern patt = Pattern.compile(pattern);
Matcher matcher = patt.matcher(s);
return matcher.group();
} catch (RuntimeException e) {
return "Error";
} }
and
String regex ="[<br/>]";
theString2=IsMatch(theString,regex);
AppLog.logString(theString2);
but But this method return always Error. Can any one tell me what's the problem?
best regads
Antonio
The problem is that you never invoke find() on your matcher. You must invoke find() before invoking group() (and test that find() returns true).
I am not sure of what your method IsMatch is supposed to do. As it is, it will either return the match (ie, "<br/>", assuming you invoke find() before) either return "Error".
Also, don't put the brackets around <br/> in your regexp, they are not needed.
I wouls really consider using replace instead of regexp for your purposes:
String s = "my dog <br/> is black and he <br/> stay on table ".replace("<br/>","");
As a recommendation, don't catch an exception without logging the it. They provide valuable information that should not be hidden. It make debug really hard when a problem occurs.
String regex ="(\<br\/\>)";
I think you need to pay attention to the grammar details about the regular expression:
1. escape the "<" like special characters
different language of regular expression have different specifications