How to use a regular expression using the model concept and match for this scenario?

Hi guys,.

I have a string "we have 7 tutorials for Java, 2 tutorials for Javascript and 1 tutorial for Oracle.

I need to replace the numbers based on the condition below.

If more then 5, replace with many

If less than 5 then replace by a few

If it is 1, replace by "only one".

Here is my code, I'm missing the part equates to replace the numbers could one of please help me solve you this.

private static String REGEX = "(\\d+)"; "

private public static String INPUT = "we have 7 tutorials for Java, 2 tutorials for Javascript and 1 tutorial for Oracle."

String pattern = "(.*) (\\d+)(.*)";

private static String REPLACE = "replace with many";

Public Shared Sub main (String [] args) {}

Create a model object

Model r = Pattern.compile ("REGEX");

Now create object match.

Matcher m = r.matcher (INPUT);

Change the value to 7 by the replacement string

How to assimilate (\\d+) greater than a number and use the code below.

ENTRY = m.replaceAll (REPLACE);

Print the final result;

System.out.println (Input);

Thank you and best regards,

Hello

Try the following makes use of 'appendReplacement"instead with the methods 'start' and 'end' to locate and check the search string"regExp"before dynamically set the string"replace ":

String regExp = "\\d+";
String input = "We have 7 tutorials for Java, 2 tutorials for Javascript and 1 tutorial for Oracle";
String replace;
Pattern p = Pattern.compile(regExp);
// get a matcher object
Matcher m = p.matcher(input);
StringBuffer sb = new StringBuffer();
while (m.find()) {
   Integer x = Integer.valueOf(input.substring(m.start(), m.end()));
   replace = (x >= 5) ? "many" : (x == 1) ? "only one" : "few";
   m.appendReplacement(sb, replace);
}
m.appendTail(sb);
System.out.println(sb.toString());

HTH.

Kind regards
Rajen

PS: Please mark as answer/useful if it solves your problem to the benefit of all members of the community.

Tags: Java

Similar Questions

  • How to use the utility disk first aid for hard drive internal el captian

    How to use the utility disk first aid for hard drive internal el captian

    Select the drive and click first aid.

    If something is not repairable because this is the system drive, then you need to start using the recovery partition and run disk utility it.

  • I pay 9.99 usd per month for the Plan of creative photography Cloud.  I use mainly LR... can I download / use the most powerful Photo Store for this award of contract also?

    I pay 9.99 usd per month for the Plan of creative photography Cloud.  I use mainly LR... can I download / use the most powerful Photo Store for this award of contract also?

    Ask for help here.

    Yes, creative photography plan cloud includes photoshop & Lightroom.

    Concerning

    Baudier

  • How to use the model taskflow

    For some of my workflow, I want to check something before running the workflow. If I I could use taskflow model but I don't really understand how it works.

    In my workflow model, I have a router that has 2 results a default result refused and the other with an expression with a result obtained. Then, I created a stubborn workflow based on this template.

    Q: how associate you the result of the router in the model entry in the btf child?

    What I don't understand is how to use the result of the router.

    Note: I want to use the flow of tasks as a reference model not copy

    Thank you

    JDev 11.1.2.4

    Try a wild-card navigation and add the result to the wildcard and point to an appropriate page.

    Timo

  • Portege R500-11Z: how to use the external mic and hear the internal speakers

    We have a Portege R500-11Z running XP and want to use Skype with an external table microphone and the internal speakers.

    When connect us the microphone to the Jack MIC the Portege, built-in speakers cut out!

    How to use an external mic and still hear the internal speakers?

    Hello

    > When connect us the microphone on the Portege, built-in speaker MIC Jack cut!
    Are you sure you used the microphone port and no headphone port?

    Have you checked your settings Audio from Skype?
    There is an option called ring through the speaker too or something like that.

    Please also check the Realtek HD audio Manager in Control Panel.
    There is a tab called Audio i/o.

    Usually, you can change the connector settings, by clicking on the small key-tool icon.
    Disable the option called Mute rear output when plugged headphones before

    As you can see is called headset and not so I m confused a little bit why the speakers are turned off when you connect an external microphone.

    By the way; There is also a tab called micro.
    Check if the two options that reduce the echo and noise are OFF

  • How to use the node function call library for a function in the dll with the data SUB type

    Hi all

    I would ask for your kind help

    I am facing a problem with the call library node.

    I have a C++ (stdcall) function, which has Sub as data type

    XXXX error code (hwnd, lid, getValue, * Sub data1, * Sub data2)

    data1 and data2 types are constantly changing based on the value of 'getValue '.

    Mainly I can use the call library node several times and adapt each node according to the types of data data1, data2 and extract the values and use in the code. Here is no question. Real question is:

    My question:

    How can I use a node of library time call and make a case according to the 'getvalue', who will control the data1, data2 data type. Here I really seeking solutions.

    My tests:

    I used varaints as entry to the libray call node of the data1, data2 and selected parameters in the call libraby node as "Adapt to type. Here labview just crashed.

    I appreciate your suggestions to feedbackand.

    Thank you

    Karine

    You must allocate enough space for data1 and data2, and then pass a pointer to this space. An easy way to do this is the function to initialize table. Set the U8 type and size for the number of bytes required. Pass this array to the function as a pointer of table data.

    After the function call returns, you need to extract the data in the table. You can do it manually, but a simple approach is to use the array of bytes to a string. Then, in a housing structure, use Unflatten chain to convert the string to the correct data type. This method also converts the "endianness" which will be probably necessary; Be sure to only set all entries for unflatten correctly.

  • I lost the ability to use the hue, Saturation and Luminance for a reason any? I now have the Calibration of the camera, which I never use. How can I get HSL back to develop the module?

    I've lost the ability to use the hue, Saturation and Luminance option in develop module. I now have the Calibration of the camera, which I never use. How to bring back HSL? Help!

    Use the menu command

    Window-> panels-> STL/color/black & white

  • How to use the find method to search for character format substitutions and FMP?

    I strongly suspect that I should be able to use the find method to search for paragraph substitutions and format as these options are available in the Find dialog box. However, because of the excruciating Extendscript documentation from Adobe, I not was able to find the key pieces of information that I need. I found these constants:

    • Constants.FV_FindPgfFormatOverride
    • Constants.FV_FindCharacterFormatOverride

    But he has literally no information about them. It is not yet a definition, so I'm actually just a guess. However, I think that I need to somehow use these constant within the parameters of the Find method. Any ideas on how to do it?

    You're right, there are still some bugs to work out of the documentation, although it has already improved since I started w / Extendscript last summer.

    Find two constants must be defined as the ival for a PropVal FindObject, for example:

    var myDoc is app. ActiveDoc;

    var docStart = myDoc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;

    tloc var = new TextLoc(docStart,0);

    var findParams = AllocatePropVals (1);

    findParams [0].propIdent.num = Constants.FS_FindObject;

    findParams [0].propVal.valType = Constants.FT_Integer;

    findParams [0].propVal.ival = Constants.FV_FindCharacterFormatOverride;

    myDoc.Find (tloc, findParams);

    Depending on what you're trying to accomplish, you can also take a look at the property FormatOverride of the Doc, bmp and item objects.

  • How to use the OS Android and Windows on the same shelf?

    Original title: is it possible I ca use the OS android and mircosoft OS?
    Is it possible that I can used both ANDROID OS and MICROSOFT OS at the same time in my Tablet?

    You will not be able to install the Windows operating system on an Android device.

  • Hey, how to use the files as in Firefox for Android?

    Hey all. Since the add-on of Ghostery for Firefox Beta for android (36) has not worked, I contacted Ghostery customer service, and they responded with a beta version of the Ghostery add-on to see if it would work. There is a file as... I was wondering, how can I use it in Firefox for android? To make use of this add-on? Thank you!

    Hi Ererer

    I have never installed an add-on third party directly to the Android operating system (only on the desktop). If you find the as in your folder Android downloads or via the Android file manager if it is in a different directory, try tapping on it and see if it will open in Firefox for Android and settle. Don't know if it will work because this cannot carry on Android and I do not see in our documentatino: https://support.mozilla.org/en-US/kb/find-and-install-add-ons-firefox-android

    Please let me know if the above works.

    See you soon!

    ... Roland

  • How to use the 2nd hard drive internal for storage?

    Hi, I am a newbie in all of this. I would use the 2nd internal hard drive of our mac mini server (end 2012) for storage (in fact, I'll things rsync from a remote server to here. but this is not the point here.). The thing is, I don't know how to access the 2nd internal hard drive and where to start. I did all of the following commands in the Terminal, but I don't really know what they mean, and what can I do with this information. I've been Googling but most are guides on how to install a 2nd hard drive etc. I hope that the information below is enough for someone to give me advice please?

    Thank you!

    That shows disk utility?

  • How to use the model instead of RP pspice to create the new component?

    Hi, I need to create the model of opa890 of TI, but I found no spice I find model pspice, how to use it to create the new template part.

    Fouda, salvation

    I took a quick glance to the component but for some reason, the model pspice does not correctly applied to the component. The second problem was that the pins have been mapped properly. I've done the component for you with the model and print and attached it to this message. You just need to add it to your database of click with the right button on the component, and then clicking on save to DB component.

    If all goes well, you should start to see the results of the component. I hope this helps.

    Best regards

  • How to use the property PivotX and PivotY in cascades

    Hello

    My goal is to rotate the line on my angle.

    1 I received two points: A(x1; y1) and B(x2; y2).

    2. I calculated the size of the vector AB and angle of point A:
    AB = Math.sqrt (Math.pow ((x2-x 1), 2) + Math.pow ((y2-y1), 2));
    Alicia = 44 *.

    3 I put in place the line on the terms of coordination (width = AB, height = 3px):
    myLine.layoutProperties.positionX = x 1;
    myLine.layoutProperties.positionY = y1;

    4 I turn the line on the angle that was found.
    The starting point of the line must be the point A, endpoint - point B. But I have no =)
    I try to use the rotationZ, but I do not know how to work with his propertyes PivotX and PivotY.
    Default 0 PivotX and PivotY = 0. When I try to set up PivotX = x 1 and y1 = PivotY I get the incorrect result.

    With PivotX = 0 and PivotY = 0 I get it (myLine.rotationZ = 44):

    Please, help me =)

    That could happen, because the pivotX and pivotY zero definition makes the rotation around the center of the element. In the case of this image, which is equal to its center point (300, 1.5), but no, it is top left. Try to set the pivotX to-(image.width/2), which will be:-300; pivotY to the-(image.height/2), which is - 1.5; and then turn again.

    Please see the details on this subject here:

    http://developer.BlackBerry.com/native/reference/Cascades/bb__cascades__visualnode.html

  • How to use the bottom of different color for the areas of text and form on a landing page?

    For our landing page, I like to keep white as the main background but color will have two text as well as a space for these three areas form box, I would use gray.  I got tired of using Fill Content but it will change the whole landing page background color and not only the text box.

    Someone knows how to do this?

    Thank you.

    Hi Angie, you use the WYSIWYG editor to create pages? If so, if I'm reading this right, you are looking to change the background color of the different page elements? You should be able to select each item and apply the color as below:

    I would like to know if this isn't what you are looking for.

    Best,

    Rob

  • How to use the Listview element and list in Adf

    Hello

    Can someone explain how to use list view and tags to list items in adf to display the data in the table.


    Concerning
    Suresh

    On the error, check your log window, if no content appears then it suggests that an error occurs.

    I built it by hand.

    CM.

Maybe you are looking for