import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(\\W)\\W+[0-9]+";
final String string = "THE WASTE LAND\n"
+ "By T. S. Eliot\n\n"
+ " \"Nam Sibyllam quidem Cumis ego ipse oculis meis\n"
+ " vidi in ampulla pendere, et cum illi pueri dicerent:\n"
+ " Σίβυλλα τί θέλεις; respondebat illa: ἀποθανεῖν θέλω.\"\n\n"
+ " For Ezra Pound\n"
+ " il miglior fabbro\n\n"
+ "I. THE BURIAL OF THE DEAD\n\n"
+ " April is the cruellest month, breeding\n"
+ " Lilacs out of the dead land, mixing\n"
+ " Memory and desire, stirring\n"
+ " Dull roots with spring rain.\n"
+ " Winter kept us warm, covering\n"
+ " Earth in forgetful snow, feeding\n"
+ " A little life with dried tubers.\n"
+ " Summer surprised us, coming over the Starnbergersee\n"
+ " With a shower of rain; we stopped in the colonnade,\n"
+ " And went on in sunlight, into the Hofgarten, 10\n"
+ " And drank coffee, and talked for an hour.\n"
+ " Bin gar keine Russin, stamm' aus Litauen, echt deutsch.\n"
+ " And when we were children, staying at the archduke's,\n"
+ " My cousin's, he took me out on a sled,\n"
+ " And I was frightened. He said, Marie,\n"
+ " Marie, hold on tight. And down we went.\n"
+ " In the mountains, there you feel free.\n"
+ " I read, much of the night, and go south in the winter.\n\n"
+ " What are the roots that clutch, what branches grow\n"
+ " Out of this stony rubbish? Son of man, 20\n"
+ " You cannot say, or guess, for you know only\n"
+ " A heap of broken images, where the sun beats,\n"
+ " And the dead tree gives no shelter, the cricket no relief,\n"
+ " And the dry stone no sound of water. Only\n"
+ " There is shadow under this red rock,\n"
+ " (Come in under the shadow of this red rock),\n"
+ " And I will show you something different from either\n"
+ " Your shadow at morning striding behind you\n"
+ " Or your shadow at evening rising to meet you;\n"
+ " I will show you fear in a handful of dust. 30\n"
+ " Frisch weht der Wind\n"
+ " Der Heimat zu\n"
+ " Mein Irisch Kind,\n"
+ " Wo weilest du?\n"
+ " \"You gave me hyacinths first a year ago;\n"
+ " \"They called me the hyacinth girl.\"\n"
+ " —Yet when we came back, late, from the Hyacinth garden,\n"
+ " Your arms full, and your hair wet, I could not\n"
+ " Speak, and my eyes failed, I was neither\n"
+ " Living nor dead, and I knew nothing, 40\n"
+ " Looking into the heart of light, the silence.\n"
+ " Oed' und leer das Meer.\n\n"
+ " Madame Sosostris, famous clairvoyante,\n"
+ " Had a bad cold, nevertheless\n"
+ " Is known to be the wisest woman in Europe,\n"
+ " With a wicked pack of cards. Here, said she,\n"
+ " Is your card, the drowned Phoenician Sailor,\n"
+ " (Those are pearls that were his eyes. Look!)\n"
+ " Here is Belladonna, the Lady of the Rocks,\n"
+ " The lady of situations. 50\n"
+ " Here is the man with three staves, and here the Wheel,\n"
+ " And here is the one-eyed merchant, and this card,\n"
+ " Which is blank, is something he carries on his back,\n"
+ " Which I am forbidden to see. I do not find\n"
+ " The Hanged Man. Fear death by water.\n"
+ " I see crowds of people, walking round in a ring.\n"
+ " Thank you. If you see dear Mrs. Equitone,\n"
+ " Tell her I bring the horoscope myself:\n"
+ " One must be so careful these days.\n\n"
+ " Unreal City, 60\n"
+ " Under the brown fog of a winter dawn,\n"
+ " A crowd flowed over London Bridge, so many,\n"
+ " I had not thought death had undone so many.\n"
+ " Sighs, short and infrequent, were exhaled,\n"
+ " And each man fixed his eyes before his feet.\n"
+ " Flowed up the hill and down King William Street,\n"
+ " To where Saint Mary Woolnoth kept the hours\n"
+ " With a dead sound on the final stroke of nine.\n"
+ " There I saw one I knew, and stopped him, crying \"Stetson!\n"
+ " \"You who were with me in the ships at Mylae! 70\n"
+ " \"That corpse you planted last year in your garden,\n"
+ " \"Has it begun to sprout? Will it bloom this year?\n"
+ " \"Or has the sudden frost disturbed its bed?\n"
+ " \"Oh keep the Dog far hence, that's friend to men,\n"
+ " \"Or with his nails he'll dig it up again!\n"
+ " \"You! hypocrite lecteur!—mon semblable,—mon frère!\"\n\n\n\n\n\n\n\n"
+ "II. A GAME OF CHESS\n\n"
+ " The Chair she sat in, like a burnished throne,\n"
+ " Glowed on the marble, where the glass\n"
+ " Held up by standards wrought with fruited vines\n"
+ " From which a golden Cupidon peeped out 80\n"
+ " (Another hid his eyes behind his wing)\n"
+ " Doubled the flames of sevenbranched candelabra\n"
+ " Reflecting light upon the table as\n"
+ " The glitter of her jewels rose to meet it,\n"
+ " From satin cases poured in rich profusion.\n"
+ " In vials of ivory and coloured glass\n"
+ " Unstoppered, lurked her strange synthetic perfumes,\n"
+ " Unguent, powdered, or liquid—troubled, confused\n"
+ " And drowned the sense in odours; stirred by the air\n"
+ " That freshened from the window, these ascended 90\n"
+ " In fattening the prolonged candle-flames,\n"
+ " Flung their smoke into the laquearia,\n"
+ " Stirring the pattern on the coffered ceiling.\n"
+ " Huge sea-wood fed with copper\n"
+ " Burned green and orange, framed by the coloured stone,\n"
+ " In which sad light a carvèd dolphin swam.\n"
+ " Above the antique mantel was displayed\n"
+ " As though a window gave upon the sylvan scene\n"
+ " The change of Philomel, by the barbarous king\n"
+ " So rudely forced; yet there the nightingale 100\n"
+ " Filled all the desert with inviolable voice\n"
+ " And still she cried, and still the world pursues,\n"
+ " \"Jug Jug\" to dirty ears.\n"
+ " And other withered stumps of time\n"
+ " Were told upon the walls; staring forms\n"
+ " Leaned out, leaning, hushing the room enclosed.\n"
+ " Footsteps shuffled on the stair.\n"
+ " Under the firelight, under the brush, her hair\n"
+ " Spread out in fiery points\n"
+ " Glowed into words, then would be savagely still. 110\n\n"
+ " \"My nerves are bad to-night. Yes, bad. Stay with me.\n"
+ " \"Speak to me. Why do you never speak. Speak.\n"
+ " \"What are you thinking of? What thinking? What?\n"
+ " \"I never know what you are thinking. Think.\"\n\n"
+ " I think we are in rats' alley\n"
+ " Where the dead men lost their bones.\n\n"
+ " \"What is that noise?\"\n"
+ " The wind under the door.\n"
+ " \"What is that noise now? What is the wind doing?\"\n"
+ " Nothing again nothing. 120\n"
+ " \"Do\n"
+ " \"You know nothing? Do you see nothing? Do you remember\n"
+ " \"Nothing?\"\n\n"
+ " I remember\n"
+ " Those are pearls that were his eyes.\n"
+ " \"Are you alive, or not? Is there nothing in your head?\"\n"
+ " But\n"
+ " O O O O that Shakespeherian Rag—\n"
+ " It's so elegant\n"
+ " So intelligent 130\n"
+ " \"What shall I do now? What shall I do?\"\n"
+ " I shall rush out as I am, and walk the street\n"
+ " \"With my hair down, so. What shall we do tomorrow?\n"
+ " \"What shall we ever do?\"\n"
+ " The hot water at ten.\n"
+ " And if it rains, a closed car at four.\n"
+ " And we shall play a game of chess,\n"
+ " Pressing lidless eyes and waiting for a knock upon the door.\n\n"
+ " When Lil's husband got demobbed, I said—\n"
+ " I didn't mince my words, I said to her myself, 140\n"
+ " HURRY UP PLEASE IT'S TIME\n"
+ " Now Albert's coming back, make yourself a bit smart.\n"
+ " He'll want to know what you done with that money he gave you\n"
+ " To get yourself some teeth. He did, I was there.\n"
+ " You have them all out, Lil, and get a nice set,\n"
+ " He said, I swear, I can't bear to look at you.\n"
+ " And no more can't I, I said, and think of poor Albert,\n"
+ " He's been in the army four years, he wants a good time,\n"
+ " And if you don't give it him, there's others will, I said.\n"
+ " Oh is there, she said. Something o' that, I said. 150\n"
+ " Then I'll know who to thank, she said, and give me a straight look.\n"
+ " HURRY UP PLEASE IT'S TIME\n"
+ " If you don't like it you can get on with it, I said.\n"
+ " Others can pick and choose if you can't.\n"
+ " But if Albert makes off, it won't be for lack of telling.\n"
+ " You ought to be ashamed, I said, to look so antique.\n"
+ " (And her only thirty-one.)\n"
+ " I can't help it, she said, pulling a long face,\n"
+ " It's them pills I took, to bring it off, she said.\n"
+ " (She's had five already, and nearly died of young George.) 160\n"
+ " The chemist said it would be all right, but I've never been the same.\n"
+ " You are a proper fool, I said.\n"
+ " Well, if Albert won't leave you alone, there it is, I said,\n"
+ " What you get married for if you don't want children?\n"
+ " HURRY UP PLEASE IT'S TIME\n"
+ " Well, that Sunday Albert was home, they had a hot gammon,\n"
+ " And they asked me in to dinner, to get the beauty of it hot—\n"
+ " HURRY UP PLEASE IT'S TIME\n"
+ " HURRY UP PLEASE IT'S TIME\n"
+ " Goonight Bill. Goonight Lou. Goonight May. Goonight. 170\n"
+ " Ta ta. Goonight. Goonight.\n"
+ " Good night, ladies, good night, sweet ladies, good night, good night.\n\n\n\n\n\n\n\n"
+ "III. THE FIRE SERMON\n\n"
+ " The river's tent is broken: the last fingers of leaf\n"
+ " Clutch and sink into the wet bank. The wind\n"
+ " Crosses the brown land, unheard. The nymphs are departed.\n"
+ " Sweet Thames, run softly, till I end my song.\n"
+ " The river bears no empty bottles, sandwich papers,\n"
+ " Silk handkerchiefs, cardboard boxes, cigarette ends\n"
+ " Or other testimony of summer nights. The nymphs are departed.\n"
+ " And their friends, the loitering heirs of city directors; 180\n"
+ " Departed, have left no addresses.\n"
+ " By the waters of Leman I sat down and wept . . .\n"
+ " Sweet Thames, run softly till I end my song,\n"
+ " Sweet Thames, run softly, for I speak not loud or long.\n"
+ " But at my back in a cold blast I hear\n"
+ " The rattle of the bones, and chuckle spread from ear to ear.\n"
+ " A rat crept softly through the vegetation\n"
+ " Dragging its slimy belly on the bank\n"
+ " While I was fishing in the dull canal\n"
+ " On a winter evening round behind the gashouse 190\n"
+ " Musing upon the king my brother's wreck\n"
+ " And on the king my father's death before him.\n"
+ " White bodies naked on the low damp ground\n"
+ " And bones cast in a little low dry garret,\n"
+ " Rattled by the rat's foot only, year to year.\n"
+ " But at my back from time to time I hear\n"
+ " The sound of horns and motors, which shall bring\n"
+ " Sweeney to Mrs. Porter in the spring.\n"
+ " O the moon shone bright on Mrs. Porter\n"
+ " And on her daughter 200\n"
+ " They wash their feet in soda water\n"
+ " Et O ces voix d'enfants, chantant dans la coupole!\n\n"
+ " Twit twit twit\n"
+ " Jug jug jug jug jug jug\n"
+ " So rudely forc'd.\n"
+ " Tereu\n\n"
+ " Unreal City\n"
+ " Under the brown fog of a winter noon\n"
+ " Mr. Eugenides, the Smyrna merchant\n"
+ " Unshaven, with a pocket full of currants 210\n"
+ " C.i.f. London: documents at sight,\n"
+ " Asked me in demotic French\n"
+ " To luncheon at the Cannon Street Hotel\n"
+ " Followed by a weekend at the Metropole.\n\n"
+ " At the violet hour, when the eyes and back\n"
+ " Turn upward from the desk, when the human engine waits\n"
+ " Like a taxi throbbing waiting,\n"
+ " I Tiresias, though blind, throbbing between two lives,\n"
+ " Old man with wrinkled female breasts, can see\n"
+ " At the violet hour, the evening hour that strives 220\n"
+ " Homeward, and brings the sailor home from sea,\n"
+ " The typist home at teatime, clears her breakfast, lights\n"
+ " Her stove, and lays out food in tins.\n"
+ " Out of the window perilously spread\n"
+ " Her drying combinations touched by the sun's last rays,\n"
+ " On the divan are piled (at night her bed)\n"
+ " Stockings, slippers, camisoles, and stays.\n"
+ " I Tiresias, old man with wrinkled dugs\n"
+ " Perceived the scene, and foretold the rest—\n"
+ " I too awaited the expected guest. 230\n"
+ " He, the young man carbuncular, arrives,\n"
+ " A small house agent's clerk, with one bold stare,\n"
+ " One of the low on whom assurance sits\n"
+ " As a silk hat on a Bradford millionaire.\n"
+ " The time is now propitious, as he guesses,\n"
+ " The meal is ended, she is bored and tired,\n"
+ " Endeavours to engage her in caresses\n"
+ " Which still are unreproved, if undesired.\n"
+ " Flushed and decided, he assaults at once;\n"
+ " Exploring hands encounter no defence; 240\n"
+ " His vanity requires no response,\n"
+ " And makes a welcome of indifference.\n"
+ " (And I Tiresias have foresuffered all\n"
+ " Enacted on this same divan or bed;\n"
+ " I who have sat by Thebes below the wall\n"
+ " And walked among the lowest of the dead.)\n"
+ " Bestows one final patronising kiss,\n"
+ " And gropes his way, finding the stairs unlit . . .\n\n"
+ " She turns and looks a moment in the glass,\n"
+ " Hardly aware of her departed lover; 250\n"
+ " Her brain allows one half-formed thought to pass:\n"
+ " \"Well now that's done: and I'm glad it's over.\"\n"
+ " When lovely woman stoops to folly and\n"
+ " Paces about her room again, alone,\n"
+ " She smooths her hair with automatic hand,\n"
+ " And puts a record on the gramophone.\n\n"
+ " \"This music crept by me upon the waters\"\n"
+ " And along the Strand, up Queen Victoria Street.\n"
+ " O City city, I can sometimes hear\n"
+ " Beside a public bar in Lower Thames Street, 260\n"
+ " The pleasant whining of a mandoline\n"
+ " And a clatter and a chatter from within\n"
+ " Where fishmen lounge at noon: where the walls\n"
+ " Of Magnus Martyr hold\n"
+ " Inexplicable splendour of Ionian white and gold.\n\n"
+ " The river sweats\n"
+ " Oil and tar\n"
+ " The barges drift\n"
+ " With the turning tide\n"
+ " Red sails 270\n"
+ " Wide\n"
+ " To leeward, swing on the heavy spar.\n"
+ " The barges wash\n"
+ " Drifting logs\n"
+ " Down Greenwich reach\n"
+ " Past the Isle of Dogs.\n"
+ " Weialala leia\n"
+ " Wallala leialala\n"
+ " Elizabeth and Leicester\n"
+ " Beating oars 280\n"
+ " The stern was formed\n"
+ " A gilded shell\n"
+ " Red and gold\n"
+ " The brisk swell\n"
+ " Rippled both shores\n"
+ " Southwest wind\n"
+ " Carried down stream\n"
+ " The peal of bells\n"
+ " White towers\n"
+ " Weialala leia 290\n"
+ " Wallala leialala\n\n"
+ " \"Trams and dusty trees.\n"
+ " Highbury bore me. Richmond and Kew\n"
+ " Undid me. By Richmond I raised my knees\n"
+ " Supine on the floor of a narrow canoe.\"\n\n"
+ " \"My feet are at Moorgate, and my heart\n"
+ " Under my feet. After the event\n"
+ " He wept. He promised 'a new start'.\n"
+ " I made no comment. What should I resent?\"\n"
+ " \"On Margate Sands. 300\n"
+ " I can connect\n"
+ " Nothing with nothing.\n"
+ " The broken fingernails of dirty hands.\n"
+ " My people humble people who expect\n"
+ " Nothing.\"\n"
+ " la la\n\n"
+ " To Carthage then I came\n\n"
+ " Burning burning burning burning\n"
+ " O Lord Thou pluckest me out\n"
+ " O Lord Thou pluckest me out 310\n\n"
+ " \n\n\n\n\n\n\n\n"
+ "IV. DEATH BY WATER\n\n"
+ " Phlebas the Phoenician, a fortnight dead,\n"
+ " Forgot the cry of gulls, and the deep sea swell\n"
+ " And the profit and loss.\n"
+ " A current under sea\n"
+ " Picked his bones in whispers. As he rose and fell\n"
+ " He passed the stages of his age and youth\n"
+ " Entering the whirlpool.\n"
+ " Gentile or Jew\n"
+ " O you who turn the wheel and look to windward, 320\n"
+ " Consider Phlebas, who was once handsome and tall as you.\n\n\n\n\n\n\n\n"
+ "V. WHAT THE THUNDER SAID\n\n"
+ " After the torchlight red on sweaty faces\n"
+ " After the frosty silence in the gardens\n"
+ " After the agony in stony places\n"
+ " The shouting and the crying\n"
+ " Prison and palace and reverberation\n"
+ " Of thunder of spring over distant mountains\n"
+ " He who was living is now dead\n"
+ " We who were living are now dying\n"
+ " With a little patience 330\n\n"
+ " Here is no water but only rock\n"
+ " Rock and no water and the sandy road\n"
+ " The road winding above among the mountains\n"
+ " Which are mountains of rock without water\n"
+ " If there were water we should stop and drink\n"
+ " Amongst the rock one cannot stop or think\n"
+ " Sweat is dry and feet are in the sand\n"
+ " If there were only water amongst the rock\n"
+ " Dead mountain mouth of carious teeth that cannot spit\n"
+ " Here one can neither stand nor lie nor sit 340\n"
+ " There is not even silence in the mountains\n"
+ " But dry sterile thunder without rain\n"
+ " There is not even solitude in the mountains\n"
+ " But red sullen faces sneer and snarl\n"
+ " From doors of mudcracked houses\n"
+ " If there were water\n"
+ " And no rock\n"
+ " If there were rock\n"
+ " And also water\n"
+ " And water 350\n"
+ " A spring\n"
+ " A pool among the rock\n"
+ " If there were the sound of water only\n"
+ " Not the cicada\n"
+ " And dry grass singing\n"
+ " But sound of water over a rock\n"
+ " Where the hermit-thrush sings in the pine trees\n"
+ " Drip drop drip drop drop drop drop\n"
+ " But there is no water\n\n"
+ " Who is the third who walks always beside you? 360\n"
+ " When I count, there are only you and I together\n"
+ " But when I look ahead up the white road\n"
+ " There is always another one walking beside you\n"
+ " Gliding wrapt in a brown mantle, hooded\n"
+ " I do not know whether a man or a woman\n"
+ " —But who is that on the other side of you?\n\n"
+ " What is that sound high in the air\n"
+ " Murmur of maternal lamentation\n"
+ " Who are those hooded hordes swarming\n"
+ " Over endless plains, stumbling in cracked earth 370\n"
+ " Ringed by the flat horizon only\n"
+ " What is the city over the mountains\n"
+ " Cracks and reforms and bursts in the violet air\n"
+ " Falling towers\n"
+ " Jerusalem Athens Alexandria\n"
+ " Vienna London\n"
+ " Unreal\n\n"
+ " A woman drew her long black hair out tight\n"
+ " And fiddled whisper music on those strings\n"
+ " And bats with baby faces in the violet light 380\n"
+ " Whistled, and beat their wings\n"
+ " And crawled head downward down a blackened wall\n"
+ " And upside down in air were towers\n"
+ " Tolling reminiscent bells, that kept the hours\n"
+ " And voices singing out of empty cisterns and exhausted wells.\n\n"
+ " In this decayed hole among the mountains\n"
+ " In the faint moonlight, the grass is singing\n"
+ " Over the tumbled graves, about the chapel\n"
+ " There is the empty chapel, only the wind's home.\n"
+ " It has no windows, and the door swings, 390\n"
+ " Dry bones can harm no one.\n"
+ " Only a cock stood on the rooftree\n"
+ " Co co rico co co rico\n"
+ " In a flash of lightning. Then a damp gust\n"
+ " Bringing rain\n\n"
+ " Ganga was sunken, and the limp leaves\n"
+ " Waited for rain, while the black clouds\n"
+ " Gathered far distant, over Himavant.\n"
+ " The jungle crouched, humped in silence.\n"
+ " Then spoke the thunder 400\n"
+ " DA\n"
+ " Datta: what have we given?\n"
+ " My friend, blood shaking my heart\n"
+ " The awful daring of a moment's surrender\n"
+ " Which an age of prudence can never retract\n"
+ " By this, and this only, we have existed\n"
+ " Which is not to be found in our obituaries\n"
+ " Or in memories draped by the beneficent spider\n"
+ " Or under seals broken by the lean solicitor\n"
+ " In our empty rooms 410\n"
+ " DA\n"
+ " Dayadhvam: I have heard the key\n"
+ " Turn in the door once and turn once only\n"
+ " We think of the key, each in his prison\n"
+ " Thinking of the key, each confirms a prison\n"
+ " Only at nightfall, aetherial rumours\n"
+ " Revive for a moment a broken Coriolanus\n"
+ " DA\n"
+ " Damyata: The boat responded\n"
+ " Gaily, to the hand expert with sail and oar 420\n"
+ " The sea was calm, your heart would have responded\n"
+ " Gaily, when invited, beating obedient\n"
+ " To controlling hands\n\n"
+ " I sat upon the shore\n"
+ " Fishing, with the arid plain behind me\n"
+ " Shall I at least set my lands in order?\n"
+ " London Bridge is falling down falling down falling down\n"
+ " Poi s'ascose nel foco che gli affina\n"
+ " Quando fiam ceu chelidon— O swallow swallow\n"
+ " Le Prince d'Aquitaine à la tour abolie 430\n"
+ " These fragments I have shored against my ruins\n"
+ " Why then Ile fit you. Hieronymo's mad againe.\n"
+ " Datta. Dayadhvam. Damyata.\n"
+ " Shantih shantih shantih";
final String subst = "\\1";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html