I have a stream from which I read a string that looks like the following:
event.tag.report tag_id=0xABCD0029605, type=ISOB_80K, antenna=1, frequency=918250, rssi=-471, tx_power=330, time=2017-12-18T19:44:07.198
^^^^^^^^^^^^^
I am trying to use Regex to just get the highlighted part (underlined by ^^^^) for every string that I read. My pattern for the Regex is as follows:
.*\\s(tag_id=)(.{38})(\\,\\s)(.*)$
However, this does not work for tag_ids which are longer than or shorter than 38 digits.
Can someone help me with a string pattern that will help me just get the highlighted area in the string independent of its size?
Looks to me as though you want all hexidecimal characters:
"tag_id=(0x[A-F0-9]+)"
So
Pattern pattern = Pattern.compile("tag_id=(0x[A-F0-9]+)");
Matcher matcher = pattern.matcher("event.tag.report tag_id=0x313532384D3135374333343435393031, type=ISOC");
if (matcher.find())
System.out.println(matcher.group(1));
returns:
0x313532384D3135374333343435393031
Related
I am trying to search the google loginform within the html code with a simple java pattern. The loginform looks like this:
<form ... id="gaia_loginform" ... > ... </form>
I am using the following pattern to find it:
Pattern pat = Pattern.compile("<form[^>]*id=[\"|']gaia_loginform[\"|'][^>]*>(.*)</form>")
Matcher mat = pat.find(html); // html is the complete website
System.out.println(mat.group(1)); // throws exception
Actually it should the contents between the two tags. Thanks for advice what I am doing wrong :)
You are misusing the Matcher. Here is how it should be used (an example of using the Matcher):
String str = "<form ... id=\"gaia_loginform\" ... >\nCONTENT\n</form>";
Pattern pat = Pattern.compile("<form\\b[^>]*\\bid=[\"']gaia_loginform[\"'][^>]*>(.*?)</form>", Pattern.DOTALL);
Matcher matcher = pat.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
See IDEONE demo
For parsing HTML you should consider using HTML parsers, even if you are not using them now.
A couple of words on the regex: I am using Pattern.DOTALL flag when declaring the regex as . should be able to match newline symbols. Tag and id names must be matches as whole words and thus I am using \\b. Instead of .* we are safer with .*? (lazy matching), it will capture as few characters as possible.
Team,
I had a task. i.e., i want to check 98% in a blcvk of data.
I trying to write some regex but its giving continuous error.
String str="OAM-2 OMFUL abmasc01 and prdrot01 98% users NB in host nus918pe locked.";
if(str.matches("[0-9][0-9]%"))
but it is returning false.
Response is truly appreciated.
Use the pattern/matcher/find method. matches applies the regex to the whole string.
Pattern pattern = Pattern.compile("[0-9]{2}%");
String test = "OAM-2 OMFUL abmasc01 and prdrot01 98% users NB in host nus918pe locked.";
Matcher matcher = pattern.matcher(test);
if(matcher.find()) {
System.out.println("Matched!");
}
Try:
str.matches(".*[0-9][0-9]%.*")
or (\d = digit):
str.matches(".*\\d\\d%.*")
The matching pattern should also match the characters that come before/after the 98% which is why you should add the .*
Comment:
You can use Pattern matcher like the others suggested, it's especially effective if you want to extract 98% out of the string - but if you're just looking to find if there's a match - I find .matches() to be simpler to use.
str.matches("[0-9][0-9]%") actually applies this regex ^[0-9][0-9]%$, which is anchored at start and end. Others have described solutions to this already.
You can try this regex \d{1,2}(\.\d{0,2})?% this will match 98% or percentage with decimal values like 98.56%as well.
Pattern pattern = Pattern.compile("\\d{1,2}(\\.\\d{0,2})?%");
String yourString= "OAM-2 OMFUL abmasc01 and prdrot01 98% users NB in host nus918pe locked.";
Matcher matcher = pattern.matcher(yourString);
while(matcher.find()) {
System.out.println(matcher.group());
}
I need help in regular expression using in regex java.
I need change group in string:
Example:
Input:
=sum($var1;2) or =if($result<10;"little";"big") ...
Need Output:
=sum(teste;2) or =if(teste<10;"little";"big") ...
Code I have:
Pattern p = Pattern.compile("(\\.*)(\\$\\w)(\\.*)");
Matcher m = p.matcher(total);
if (m.find()) {
System.out.println(m.replaceAll("$2teste"));
}
Output I have:
=sum($vtestear1;2)
=if($r testeesultado<5;"maior";"menor")
Why match everything when all you need is to match variable tokens?
Pattern p = Pattern.compile("\\b\\$[a-z0-9]+\\b");
p.matcher(total).replaceAll("teste");
Change the [a-z0-9] part if you can have more than lowercase ASCII letters and digits.
Also, you don't need to test for .find() or anything if you .replace(): no match means nothing will be replaced.
I'm trying to extract the text BetClic from this string popup_siteinfo(this, '/click/betclic', '373', 'BetClic', '60€');
I wrote a simple regex that works on Regex Tester but that doesn't work on Java.
Here's the regex
'\d+', '(.*?)'
here's Java output
Exception in thread "main" java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:485)
at javaapplication1.JavaApplication1.main(JavaApplication1.java:74)
Java Result: 1
and here's my code
Pattern pattern = Pattern.compile("'\\d+', '(.*?)'");
Matcher matcher = pattern.matcher(onMouseOver);
System.out.print(matcher.group(1));
where the onMouseOver string is popup_siteinfo(this, '/click/betclic', '373', 'BetClic', '60€');
I'm not an expert with regex, but I'm quite sure that mine isn't wrong at all!
Suggestions?
You need to call find() before group(...):
Pattern pattern = Pattern.compile("'\\d+', '(.*?)'");
Matcher matcher = pattern.matcher(onMouseOver);
if(matcher.find()) {
System.out.print(matcher.group(1));
}
else {
System.out.print("no match");
}
You're calling group(1) without having first called a matching operation (such as find()).- which is the cause of IllegalStateException.
And if you have to use that grouped cases for replacement then this isn't needed if you're just using $1 since the replaceAll() is the matching operation.
I have the following code that can replace the email address in a String in Java:
addressStr.replaceFirst("([a-zA-Z0-9_\\-\\.]+)#((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})", "")
So, a string with John Smith <john#smith.com> would become John Smith <>. How do I negate it so that it will instead replace all that doesn't match the email address and have the final result as just john#smith.com?
I tried to put in the ^ and ?<= at the front but it doesn't work.
Well, it's not the regex you need to change but the calling code. Your regex matches the e-mail address (in a weird way), and the replace() removes it from the string.
So just use
Pattern regex = Pattern.compile("([a-zA-Z0-9_\\-\\.]+)#((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})");
Matcher regexMatcher = regex.matcher(addressStr);
if (regexMatcher.find()) {
address = regexMatcher.group();
}
The complete Java regex for catching e-mails would be as follows:
"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")#(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"
Take a look at https://www.rfc-editor.org/rfc/rfc2822#section-3.4.1 for more info on this.
A bit complicated but it is valid for all known and valid emails formats (yours do not allows mails like bob+bib#gmail.com which are valid).
For your problem, as stated multiple times, just find (stealing Tim Pietzcker piece of code):
Pattern regex = Pattern.compile("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")#(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])");
Matcher regexMatcher = regex.matcher(addressStr);
foundMatch = regexMatcher.find();
You can try:
String mailId = Pattern.compile(regexp, Pattern.LITERAL).matcher(addressStr).group();
Idea here is to get the matched string rather than trying to replace everything else with blank. You can extract the pattern into a field if this operation is repetitive.
Just don't replace.... use match(es) instead.