Here is the message and Type
| message | Type |
|---|---|
| IND SMD 0402 1.2nH 50pH 390mA 100MOHM | MAG |
What i'm trying to return is the 1.2nH
For that i created a function as below
public static String SplitStringValue1(String message,String type ) {
String[] SplitMessage = message.split(" ");
String ch="";
if (type.equals("MAG"))
{
if (SplitMessage[0].matches("IND\\d(.*)")||SplitMessage[0].equals("IND")||SplitMessage[0].matches("SELF\\d(.*)")||SplitMessage[0].equals("SELF"))
{
if (SplitMessage[1].equals("SMD"))
{
for (int j=0;j<SplitMessage.length;j )
{
if ((SplitMessage[j].matches("(-)*\\d*(\\.)?\\d*(\\/)?\\d*(.)?d*(UH|uH|MH|mH|nH|NH|H|h)")&&SplitMessage[j].length()<15))
{
ch=SplitMessage[j];
}
}
}
}
}
return ch;
}
This is returning me 50pH but actually i'm trying to return 1.2nH
How could i correct my function ?
CodePudding user response:
If you use the code to check if the type is MAG, you can use a bit more specific pattern to get the value 1.2nH with a single capture group
^IND\b.*?\h(\d*\.?\d (?:(?:[UuMmNn])?H|h))\b
Explanation
^Start of stringIND\bMatch the word IND.*?\hMatch as least as possible chars and then a space(Capture group 1\d*\.?\dMatch 1 digits with an option decimal part(?:(?:[UuMmNn])?H|h)MatchUHuHetc.. or justHorh
)Close group 1\bA word boundary
In Java
String regex = "^IND\\b.*?\\h(\\d*\\.?\\d (?:(?:[UuMmNn])?H|h))\\b";
See a Java demo
